Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
18 views

Sorting Algorithm (Group 3)

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Sorting Algorithm (Group 3)

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

SORTING

ALGORITHM
 PRESENTED BY GROUP 3
z
What is Sorting?

 Sorting refers to arranging data in a
particular format. Sorting algorithm specifies
the way to arrange data in a particular order.
Most common orders are in numerical or
lexicographical order.
z
There are two different categories in
sorting:

 Internal sorting: If the input data is such that it can


be adjusted in the main memory at once, it is called
internal sorting.

 External sorting: If the input data is such that it


cannot be adjusted in the memory entirely at once, it
needs to be stored in a hard disk, floppy disk, or any
other storage device. This is called external sorting.
z
Why Sorting Algorithm is Important? 

 A sorting algorithm will put items in a list into


an order, such as alphabetical or numerical
order. Sorting a list of items can take a long
time, especially if it is a large list. ... A
computer program can be created to do this,
making sorting a list of data much easier.
z
TYPES OF SORTING ALGORITHM:

• BUBBLE SORTING

• MERGE SORTING

• QUICK SORTING
z
WHAT IS BUBBLE SORTING?

 Bubble sort, sometimes referred to as


sinking sort, is a simple sorting algorithm that
repeatedly steps through the list, compares
adjacent elements and swaps them if they are
in the wrong order. The pass through the list is
repeated until the list is sorted.
z
Bubble Sort is the simplest sorting algorithm
that works by repeatedly swapping the
adjacent elements if they are in wrong order.

 Example:
First Pass:
( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first
two elements, and swaps since 5 > 1.
( 1 5 4 2 8 ) –>  ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) –>  ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ), Now, since these elements are
already in order (8 > 5), algorithm does not swap them.
z

 Second Pass:
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 )
( 1 4 2 5 8 ) –> ( 1 2 4 5 8 ), Swap since 4 > 2
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –>  ( 1 2 4 5 8 )

Now, the array is already sorted, but our algorithm


does not know if it is completed. The algorithm needs
one whole pass without any swap to know it is
sorted.
z

 Third Pass:
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )

Due to its simplicity, bubble sort is often used to introduce


the concept of a sorting algorithm.
In computer graphics it is popular for its capability to
detect a very small error (like swap of just two elements)
in almost-sorted arrays and fix it with just linear
complexity 
z
WHAT IS MERGE SORTING?

 Merge sort is one of the most


efficient sorting algorithms. It works on the
principle of Divide and Conquer. Merge sort
first divides the array into equal halves and
then combines them in a sorted manner.
z

How Merge Sort Works?


To understand merge sort, we take an unsorted array as
the following 
z
We know that merge sort first divides the whole array
iteratively into equal halves unless the atomic values are
achieved. We see here that an array of 8 items is divided
into two arrays of size 4.

1.

2.
z
This does not change the sequence of appearance of items
in the original. Now we divide these two arrays into halves.

2.

3.
z
We further divide these arrays and we achieve atomic value which
can no more be divided.

3.

4.
z

 Now, we combine them in exactly the


same manner as they were broken
down. Please note the color codes
given to these lists.
z
We first compare the element for each list and then combine them
into another list in a sorted manner. We see that 14 and 33 are in
sorted positions. We compare 27 and 10 and in the target list of 2
values we put 10 first, followed by 27. We change the order of 19
and 35 whereas 42 and 44 are placed sequentially.

4.

5.
z
In the next iteration of the combining phase, we
compare lists of two data values, and merge them
into a list of found data values placing all in a sorted
order.

5.

6.
z
After the final merging, the list should look
like this:

6.

7.
z
WHAT IS QUICK SORTING?

 Quick sort is a highly efficient sorting algorithm


and is based on partitioning of array of data into
smaller arrays. A large array is partitioned into
two arrays one of which holds values smaller
than the specified value, say pivot, based on
which the partition is made and another array
holds values greater than the pivot value.
z
Partition in Quick Sort
z
Quick Sort Algorithm: Steps on how it works:

1. Find a “pivot” item in the array. This item is the basis for
comparison for a single round.

2. Start a pointer (the left pointer) at the first item in the array.

3. Start a pointer (the right pointer) at the last item in the array.

4. While the value at the left pointer in the array is less than the
pivot value, move the left pointer to the right (add 1). Continue until
the value at the left pointer is greater than or equal to the pivot
value.
z
Quick Sort Algorithm: Steps on how it works:

5. While the value at the right pointer in the array is greater than the pivot value,
move the right pointer to the left (subtract 1). Continue until the value at the right
pointer is less than or equal to the pivot value.

6. If the left pointer is less than or equal to the right pointer, then swap the values
at these locations in the array.

7. Move the left pointer to the right by one and the right pointer to the left by one.

8. If the left pointer and right pointer don’t meet, go to step 1.


z
Below is an image of an array, which needs to be sorted.
We will use the Quick Sort Algorithm, to sort this array:
z
z
The pivot value divides the list into two parts. And recursively,
we find the pivot for each sub-lists until all lists contains only
one element.
END.

You might also like