SlideShare a Scribd company logo
DATA STRUCTURES
Sorting
SORTING
 A process that organizes a collection of
data into either ascending or descending
order.
 Can be used as a first step for searching
the data.
 Binary Search required a sorted array.
SORTING ALGORITHMS
 Selection Sort
 Insertion Sort
 Bubble Sort
 Quick Sort
 Merge Sort
 Heap Sort
SELECTION SORT
 It is simple and easy to implement
 It is inefficient for large list, usually used to
sort lists of no more than 1000 items
 In array of n elements, n-1 iterations are
required to sort the array
SELECTION SORT
 Select the smallest value from the list.
 Bring it to the first location of the list.
 Find the next value and repeat the
process by swapping the locations.
SELECTION SORT
 Suppose the name of the array is A and it
has four elements with the following
values:
4 19 1 3
 To sort this array in ascending order, n-1,
i.e. three iterations will be required.
SELECTION SORT
 Iteration-1
The array is scanned starting from the first to the last element and the
element that has the smallest value is selected. The smallest value is 1
at location 3. The address of element that has the smallest value is
noted and the selected value is interchanged with the first element i.e.
A[1] and A[3] are swapped
4 19 1 3
1 19 4 3
SELECTION SORT
 Iteration-2
The array is scanned starting from the second to the last element and
the element that has the smallest value is selected. The smallest value
is 3 at location 4. The address of element that has the smallest value is
noted. The selected value is interchanged with the second element i.e.
A[2] and A[4] are swapped
1 19 4 3
1 3 4 19
SELECTION SORT
 Iteration-3
The array is scanned starting from the third to the last element and the
element that has the smallest value is selected. The smallest value is 4
at location 3. The address of element that has the smallest value is
noted. The selected value is interchanged with the third element i.e.
A[3] and A[3] are swapped
1 3 4 19
1 3 4 19
ANOTHER EXAMPLE: SELECTION SORT
 26 33 43 100 46 88 52 17 53 77
 17 | 33 43 100 46 88 52 26 53 77
 17 26 | 43 100 46 88 52 33 53 77
 17 26 33 | 100 46 88 52 43 53 77
 17 26 33 43 | 46 88 52 100 53 77
 17 26 33 43 46 | 88 52 100 53 77
 17 26 33 43 46 52 | 88 100 53 77
 17 26 33 43 46 52 53 | 100 88 77
 17 26 33 43 46 52 53 77 | 88 100
 17 26 33 43 46 52 53 77 88 | 100
SELECTION SORT
void selectionSort(int numbers[ ], int array_size)
{
int i, j;
int min, temp;
for (i = 0; i < array_size-1; i++)
{
min = i;
for (j = i+1; j < array_size; j++)
{
if (numbers[j] < numbers[min])
min = j;
}
temp = numbers[i];
numbers[i] = numbers[min];
numbers[min] = temp;
}
}
BUBBLE SORT
 It is the oldest and simplest method and can be
easily implemented
 It is also the slowest and considered to be the most
inefficient sorting algorithm
 It works by comparing each item in the list with the
item next to it, and swapping them if required
 It is used only a small amount of data
BUBBLE SORT
1. First Iteration
1. Starting from the first index, compare the first and the second
elements.
2. If the first element is greater than the second element, they are
swapped.
3. Now, compare the second and the third elements. Swap them if
they are not in order.
4. The above process goes on until the last element.
2. The same process goes on for the remaining
iterations.
BUBBLE SORT
 To sort data in an array of n elements, n-1 iterations are required
 Following steps explain sorting of data in an array in acceding order
 In first iteration, the largest value moves to the last position in the array
 In second iteration, the above process is repeated and the second largest
value moves to the second last position in the array and so on
 In n-1 iteration, the data is arranged in ascending order
BUBBLE SORT
 Suppose the name of the array is A and it
has four elements with the following
values:
4 19 1 3
 To sort this array in ascending order, n-1,
i.e. three iterations will be required.
BUBBLE SORT
 Iteration-1
 A[1] is compared with element A[2]. Since 4 is not greater than 19. there will be no
change in the list.
 A[2] is compared with element A[3]. Since 19 is greater than 1, the value are
interchanged
 A[3] is compared with element A[4]. Since 19 is grater than 3, the value are
interchanged
 Thus at the end of the first iteration, the largest value moves to the last
position in the array
4 19 1 3
4 1 19 3
4 1 3 19
BUBBLE SORT
 Iteration-2
 A[1] is compared with element A[2]. Since 4 is greater than 1, the value are
interchanged
 A[2] is compared with element A[3]. Since 4 is grater than 3, the value are
interchanged
 Thus at the end of the second iteration, the second largest value moves
to the second last position in the array
1 4 3 19
1 3 4 19
BUBBLE SORT
 Iteration-3
 A[1] is compared with element A[2]. Since 1 is not greater than 3, the value are not
interchanged
 So array is sorted in ascending order
1 3 4 19
BUBBLE SORT
 Bubble sort is similar to selection
sort in the sense that it repeatedly
finds the largest/smallest value in the
unprocessed portion of the array and
puts it back.
 However, finding the largest
value is not done by selection this
time.
 We "bubble" up the largest value
instead.
BUBBLE SORT
 Compares adjacent items and exchanges them
if they are out of order.
 Comprises of several passes.
 In one pass, the largest value has been
“bubbled” to its proper position.
 In second pass, the last value does not need to
be compared.
BUBBLE SORT
void bubbleSort (int a[ ], int n)
{
int i, j, temp;
for(int i = 0; i < n-1; i++)
{
for(int j=0; j < (n-1)-i; j++)
{
if(a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
}
BUBBLE SORT EXAMPLE
9, 6, 2, 12, 11, 9, 3, 7
6, 9, 2, 12, 11, 9, 3, 7
6, 2, 9, 12, 11, 9, 3, 7
6, 2, 9, 12, 11, 9, 3, 7
6, 2, 9, 11, 12, 9, 3, 7
6, 2, 9, 11, 9, 12, 3, 7
6, 2, 9, 11, 9, 3, 12, 7
6, 2, 9, 11, 9, 3, 7, 12
BUBBLE SORT EXAMPLE
6, 2, 9, 11, 9, 3, 7, 12
2, 6, 9, 11, 9, 3, 7, 12
2, 6, 9, 9, 11, 3, 7, 12
2, 6, 9, 9, 3, 11, 7, 12
2, 6, 9, 9, 3, 7, 11, 12
6, 2, 9, 11, 9, 3, 7, 12
Notice that this time we do not have to compare the last two numbers as we
know the 12 is in position. This pass therefore only requires 6 comparisons.
First Pass
Second Pass
BUBBLE SORT EXAMPLE
2, 6, 9, 9, 3, 7, 11, 12
2, 6, 9, 3, 9, 7, 11, 12
2, 6, 9, 3, 7, 9, 11, 12
6, 2, 9, 11, 9, 3, 7, 12
2, 6, 9, 9, 3, 7, 11, 12
Second Pass
First Pass
Third Pass
This time the 11 and 12 are in position. This pass therefore only requires 5
comparisons.
BUBBLE SORT EXAMPLE
2, 6, 9, 3, 7, 9, 11, 12
2, 6, 3, 9, 7, 9, 11, 12
2, 6, 3, 7, 9, 9, 11, 12
6, 2, 9, 11, 9, 3, 7, 12
2, 6, 9, 9, 3, 7, 11, 12
Second Pass
First Pass
Third Pass
Each pass requires fewer comparisons. This time only 4 are needed.
2, 6, 9, 3, 7, 9, 11, 12
Fourth Pass
BUBBLE SORT EXAMPLE
2, 6, 3, 7, 9, 9, 11, 12
2, 3, 6, 7, 9, 9, 11, 12
6, 2, 9, 11, 9, 3, 7, 12
2, 6, 9, 9, 3, 7, 11, 12
Second Pass
First Pass
Third Pass
The list is now sorted but the algorithm does not know this until it completes a
pass with no exchanges.
2, 6, 9, 3, 7, 9, 11, 12
Fourth Pass
2, 6, 3, 7, 9, 9, 11, 12
Fifth Pass
INSERTION SORT
 It is simple as the bubble sort but it is almost
twice as efficient as the bubble sort
 It is relatively simple and easy to implement
 It is inefficient for large lists
INSERTION SORT
 In insertion sorting, the list or array is scanned from the
beginning to the end
 In each iteration, one element is inserted into its correct
position relative to the previously sorted elements of the list
 The array elements are not swapped or interchanged
 They are shifted towards the right of the list to make room
for the new element to be inserted
INSERTION SORT
 Given an unsorted list.
 Partition the list into two regions: sorted &
unsorted.
 At each step, take the first item from
unsorted and place it into its correct
position.
 Also requires to shift the remaining items
to make a room for the inserted item.
INSERTION SORT
 Suppose the name of the array is A and it
has six elements with the following
values:
16 17 2 8 18 1
 To sort this array in ascending order, six
iterations will be required.
INSERTION SORT
 Iteration-1
A[1] is compared with itself and it is not shifted. The array A remains the
same
16 17 2 8 18 1
16 17 2 8 18 1
INSERTION SORT
 Iteration-2
All data of elements on left of A[2] that are greater than A[2] are shifted
one position to the right to make room for A[2] to insert its data into the
correct location.
There is only one element with value 16 to the left of A[2]. Thus no
shifting takes place because 16 is less than 17. So A[1] and A[2] are in
correct position relative to each other. The array A remains same
16 17 2 8 18 1
16 17 2 8 18 1
INSERTION SORT
 Iteration-3
All data of elements on left of A[3] that are greater than A[3] are shifted
one position to the right to make room for A[3] to insert its data into the
correct location.
There is two elements of left side of A[3] and both are greater than A[3].
Thus shift data A[1] & A[2] one position to right and insert the value of
A[3] at A[1]. The array A after shifting and inserting value is:
16 17 2 8 18 1
2 16 17 8 18 1
INSERTION SORT
 Iteration-4
All data of elements on left of A[4] that are greater than A[4] are shifted
one position to the right to make room for A[4] to insert its data into the
correct location.
There is three elements of left side of A[4] and A[2] & A[3] are greater
than A[4]. Thus shift data A[2] & A[3] one position to right and insert the
value of A[4] at A[2]. The array A after shifting and inserting value is:
2 16 17 8 18 1
2 8 16 17 18 1
INSERTION SORT
 Iteration-5
All data of elements on left of A[5] that are greater than A[5] are shifted
one position to the right to make room for A[5] to insert its data into the
correct location.
There is four elements of left side of A[5] and all are less than A[5]. Thus
no shifting & insertion takes place. The array A remains same:
2 8 16 17 18 1
2 8 16 17 18 1
INSERTION SORT
 Iteration-6
All data of elements on left of A[6] that are greater than A[6] are shifted one
position to the right to make room for A[6] to insert its data into the correct
location.
There is five elements of left side of A[6] and all are greater than A[6]. Thus shift
data of each element from A[1] to A[5] one position to right and insert the value
of A[6] at A[1]. The array A after shifting and inserting value is:
2 8 16 17 18 1
1 2 8 16 17 18
ALGORITHM – INSERTION SORT
InsertionSort()
Algorithm to sort an array A consisting of N elements in ascending order
1. Start
2. Repeat step 3 to 8 For C = 2 to N
3. Set Temp = A[C]
4. Set L = C
5. Repeat Step 6 to 7 While (L>1 and Temp<=A[L-1])
6. Set A[L] = A[L-1]
7. L = L – 1
8. Set A[L] = Temp
9. Exit
INSERTION SORT CONT…..
• The insertion sort algorithm sorts the list by moving
each element to its proper place
Figure 6: Array list to be sorted
Figure 7: Sorted and unsorted portions of the array list
INSERTION SORT ALGORITHM (CONT’D)
Figure 8: Move list[4] into list[2]
Figure 9: Copy list[4] into temp
INSERTION SORT ALGORITHM (CONT’D)
Figure 10: Array list before copying list[3] into list[4], then
list[2] into list[3]
Figure 11: Array list after copying list[3] into list[4], and then
list[2] into list[3]
INSERTION SORT ALGORITHM (CONT’D)
Figure 12: Array list after copying temp into list[2]
AN EXAMPLE: INSERTION SORT
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
30 10 40 20
1 2 3 4
i =  j =  key = 
A[j] =  A[j+1] = 
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
30 10 40 20
1 2 3 4
i = 2 j = 1 key = 10
A[j] = 30 A[j+1] = 10
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
30 30 40 20
1 2 3 4
i = 2 j = 1 key = 10
A[j] = 30 A[j+1] = 30
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
30 30 40 20
1 2 3 4
i = 2 j = 1 key = 10
A[j] = 30 A[j+1] = 30
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
30 30 40 20
1 2 3 4
i = 2 j = 0 key = 10
A[j] =  A[j+1] = 30
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
30 30 40 20
1 2 3 4
i = 2 j = 0 key = 10
A[j] =  A[j+1] = 30
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 20
1 2 3 4
i = 2 j = 0 key = 10
A[j] =  A[j+1] = 10
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 20
1 2 3 4
i = 3 j = 0 key = 10
A[j] =  A[j+1] = 10
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 20
1 2 3 4
i = 3 j = 0 key = 40
A[j] =  A[j+1] = 10
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 20
1 2 3 4
i = 3 j = 0 key = 40
A[j] =  A[j+1] = 10
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 20
1 2 3 4
i = 3 j = 2 key = 40
A[j] = 30 A[j+1] = 40
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 20
1 2 3 4
i = 3 j = 2 key = 40
A[j] = 30 A[j+1] = 40
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 20
1 2 3 4
i = 3 j = 2 key = 40
A[j] = 30 A[j+1] = 40
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 20
1 2 3 4
i = 4 j = 2 key = 40
A[j] = 30 A[j+1] = 40
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 20
1 2 3 4
i = 4 j = 2 key = 20
A[j] = 30 A[j+1] = 40
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 20
1 2 3 4
i = 4 j = 2 key = 20
A[j] = 30 A[j+1] = 40
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 20
1 2 3 4
i = 4 j = 3 key = 20
A[j] = 40 A[j+1] = 20
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 20
1 2 3 4
i = 4 j = 3 key = 20
A[j] = 40 A[j+1] = 20
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 40
1 2 3 4
i = 4 j = 3 key = 20
A[j] = 40 A[j+1] = 40
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 40
1 2 3 4
i = 4 j = 3 key = 20
A[j] = 40 A[j+1] = 40
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 40
1 2 3 4
i = 4 j = 3 key = 20
A[j] = 40 A[j+1] = 40
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 40
1 2 3 4
i = 4 j = 2 key = 20
A[j] = 30 A[j+1] = 40
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 40
1 2 3 4
i = 4 j = 2 key = 20
A[j] = 30 A[j+1] = 40
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 30 40
1 2 3 4
i = 4 j = 2 key = 20
A[j] = 30 A[j+1] = 30
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 30 40
1 2 3 4
i = 4 j = 2 key = 20
A[j] = 30 A[j+1] = 30
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 30 40
1 2 3 4
i = 4 j = 1 key = 20
A[j] = 10 A[j+1] = 30
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 30 40
1 2 3 4
i = 4 j = 1 key = 20
A[j] = 10 A[j+1] = 30
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 20 30 40
1 2 3 4
i = 4 j = 1 key = 20
A[j] = 10 A[j+1] = 20
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 20 30 40
1 2 3 4
i = 4 j = 1 key = 20
A[j] = 10 A[j+1] = 20
Done!
EXAMPLE: INSERTION SORT
 99 | 55 4 66 28 31 36 52 38 72
 55 99 | 4 66 28 31 36 52 38 72
 4 55 99 | 66 28 31 36 52 38 72
 4 55 66 99 | 28 31 36 52 38 72
 4 28 55 66 99 | 31 36 52 38 72
 4 28 31 55 66 99 | 36 52 38 72
 4 28 31 36 55 66 99 | 52 38 72
 4 28 31 36 52 55 66 99 | 38 72
 4 28 31 36 38 52 55 66 99 | 72
 4 28 31 36 38 52 55 66 72 99 |
INSERTION SORT ALGORITHM
void insertionSort(int array[], int length)
{
int i, j, value;
for(i = 1; i < length; i++)
{
value = a[i];
for (j = i - 1; j >= 0 && a[ j ] > value; j--)
{
a[j + 1] = a[ j ];
}
a[j + 1] = value;
}
}
MERGE SORT
 Merge sort is a sorting algorithm for
rearranging lists (or any other data
structure that can only be accessed
sequentially) into a specified order.
 It is a particularly good example of the
divide and conquer algorithmic paradigm.
MERGE SORT
 Conceptually, merge sort works as follows:
Divide the unsorted list into two sub-lists
of about half the size.
Sort each of the two sub-lists.
Merge the two sorted sub-lists back into
one sorted list.
MERGE SORT
Array mergeSort(Array m)
Array left, right.
if length(m) ≤ 1
return m
else
middle = length(m) / 2
for each x in m up to middle
add x to left
for each x in m after middle
add x to right
left = mergesort(left)
right = mergesort(right)
result = merge(left, right)
return result
MERGE SORT
Array merge(left,right)
Array result
while length(left) > 0 and length(right) > 0
if first(left) ≤ first(right)
append first(left) to result
left = rest(left)
else
append first(right) to result
right = rest(right)
if length(left) > 0
append left to result
if length(right) > 0
append right to result
return result
EXECUTION EXAMPLE
Execution Example
Execution Example
Execution Example
Execution Example
Execution Example
Execution Example
Execution Example
Execution Example
Execution Example
MERGE SORT ANOTHER EXAMPLE
 Suppose the name of the array is AB and
it has six elements with the following
values:
16 17 2 8 18 1
 To sort this array in ascending order
MERGE SORT
AB
 Divide array AB into two sub-arrays A & B
A B
 Sort A & B using Bubble or selection or insertion sort
A B
16 17 2 8 18 1
16 17 2 8 18 1
2 16 17 1 8 18
MERGE SORT
A B
 Compare A[1] to B[1], so B[1] is less than A[1], the value of B[1] is
move to AB[1]
AB
1
2 16 17 1 8 18
MERGE SORT
A B
 Compare A[1] to B[2], so A[1] is less than B[2], the value of A[2] is
move to AB[2]
AB
1 2
2 16 17 1 8 18
MERGE SORT
A B
 Compare A[2] to B[2], so B[2] is less than A[2], the value of B[2] is
move to AB[3]
AB
1 2 8
2 16 17 1 8 18
MERGE SORT
A B
 Compare A[2] to B[3], so A[2] is less than B[3], the value of A[2] is
move to AB[4]
AB
1 2 8 16
2 16 17 1 8 18
MERGE SORT
A B
 Compare A[3] to B[3], so A[3] is less than B[3], the value of A[3] is
move to AB[5]
AB
 At the end, B[3] is move to AB[6], array is sorted
AB
1 2 8 16 17
2 16 17 1 8 18
1 2 8 16 17 18
ALGORITHM
Mergesort(Passed an array)
if array size > 1
Divide array in half
Call Mergesort on first half.
Call Mergesort on second half.
Merge two halves.
Merge(Passed two arrays)
Compare leading element in each array
Select lower and place in new array.
(If one input array is empty then place
remainder of other array in output array)
LB
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98
Merge
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98
23
Merge
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98
23 98
Merge
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
23 98
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
Merge
23 98
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
14
Merge
23 98
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
45
Merge
23 98 14
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
Merge
98 45
14
23
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
Merge
98 14
14
23 45
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
Merge
23 14
14 23
98 45
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
Merge
23 98 45
14
14 23 45
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
Merge
23 98 45
14
14 23 45 98
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
23 98 45
14
14 23 45 98
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6
23 98 45
14
14 23 45 98
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6
Merge
23 98 45
14
14 23 45 98
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6
6
Merge
23 98 45
14
14 23 45 98
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6
67
Merge
23 98 45
14 6
14 23 45 98
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6 33 42
23 98 45
14 67
6
14 23 45 98
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6 33 42
Merge
23 98 45
14 67
6
14 23 45 98
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6 33 42
Merge
33
23 98 45
14 67
6
14 23 45 98
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6 33 42
Merge
42
23 98 45
14 67
6 33
14 23 45 98
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6 33 42
Merge
23 98 45
14 67
6 42
33
14 23 45 98
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6 33 42
Merge
23 98 45
14 6 42
33
14 23 45 98 6
67
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6 33 42
Merge
23 98 45
14 6 33
14 23 45 98 6 33
67 42
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6 33 42
Merge
23 98 45
14 6 42
33
14 23 45 98 6 33 42
67
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6 33 42
Merge
23 98 45
14 67
6 42
33
14 23 45 98 6 33 42 67
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6 33 42
Merge
23 98 45
14 67
6 42
33
23 45 98 33 42 67
14 6
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6 33 42
Merge
23 98 45
14 67
6 42
33
23 45 98 6 42 67
6
14 33
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6 33 42
Merge
23 98 45
14 67
6 42
33
14 45 98 6 42 67
6 14
23 33
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6 33 42
Merge
23 98 45
14 67
6 42
33
14 23 98 6 42 67
6 14 23
45 33
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6 33 42
Merge
23 98 45
14 67
6 42
33
14 23 98 6 33 67
6 14 23 33
45 42
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6 33 42
Merge
23 98 45
14 67
6 42
33
14 23 98 6 33 42
6 14 23 33 42
45 67
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6 33 42
Merge
23 98 45
14 67
6 42
33
14 23 45 6 33 42
6 14 23 33 42 45
98 67
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6 33 42
Merge
23 98 45
14 67
6 42
33
14 23 45 98 6 33 42 67
6 14 23 33 42 45 67
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6 33 42
Merge
23 98 45
14 67
6 42
33
14 23 45 98 6 33 42 67
6 14 23 33 42 45 67 98
67
45
23 14 6 33
98 42
67
45
23 14 6 33
98 42
45
23 14
98
23
98 45 14
67
6 33 42
67
6 33 42
23 98 45
14 67
6 42
33
14 23 45 98 6 33 42 67
6 14 23 33 42 45 67 98
67
45
23 14 6 33
98 42
6 14 23 33 42 45 67 98
QUICK SORT
 Quick sort sorts by employing a divide and conquer
strategy to divide a list into two sub-lists.
 The steps are:
 Pick an element, called a pivot, from the list.
 Reorder the list so that all elements which are less
than the pivot come before the pivot and so that all
elements greater than the pivot come after it (equal
values can go either way).
 After this partitioning, the pivot is in its final
position. This is called the partition operation.
 Recursively sort the sub-list of lesser elements and
the sub-list of greater elements.
QUICK SORT
 Choose the appropriate pivot, either
randomly or near the median of the array
elements.
 Avoid a pivot which makes either of the
two halves empty.
EXAMPLE
We are given array of n integers to sort:
40 20 10 80 60 50 7 30 100
PICK PIVOT ELEMENT
There are a number of ways to pick the pivot element.
In this example, we will use the first element in the
array:
40 20 10 80 60 50 7 30 100
PARTITIONING ARRAY
Given a pivot, partition the elements of the array
such that the resulting array consists of:
1. One sub-array that contains elements >= pivot
2. Another sub-array that contains elements < pivot
The sub-arrays are stored in the original data
array.
Partitioning loops through, swapping elements
below/above pivot.
40 20 10 80 60 50 7 30 100
pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Too_big_index
too_small_index
40 20 10 80 60 50 7 30 100
pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
40 20 10 80 60 50 7 30 100
pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
40 20 10 80 60 50 7 30 100
pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
40 20 10 80 60 50 7 30 100
pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
40 20 10 80 60 50 7 30 100
pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
40 20 10 80 60 50 7 30 100
pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index > too_small_index
swap data[too_big_index] and data[too_small_index]
40 20 10 30 60 50 7 80 100
pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index > too_small_index
swap data[too_big_index] and data[too_small_index]
40 20 10 30 60 50 7 80 100
pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index > too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
40 20 10 30 60 50 7 80 100
pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index > too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
40 20 10 30 60 50 7 80 100
pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index > too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
40 20 10 30 60 50 7 80 100
pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index > too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
40 20 10 30 60 50 7 80 100
pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index > too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
40 20 10 30 60 50 7 80 100
pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index > too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index > too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
40 20 10 30 7 50 60 80 100
pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index > too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
40 20 10 30 7 50 60 80 100
pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index > too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
40 20 10 30 7 50 60 80 100
pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index > too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
40 20 10 30 7 50 60 80 100
pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index > too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
40 20 10 30 7 50 60 80 100
pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index > too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
40 20 10 30 7 50 60 80 100
pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index > too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
40 20 10 30 7 50 60 80 100
pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index > too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
40 20 10 30 7 50 60 80 100
pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index > too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
40 20 10 30 7 50 60 80 100
pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index > too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
5. Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100
pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot]
++too_big_index
2. While data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index > too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
5. Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100
pivot_index = 4
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
PARTITION RESULT
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
<= data[pivot] > data[pivot]
RECURSION: QUICKSORT SUB-ARRAYS
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
<= data[pivot] > data[pivot]
QUICK SORT
function quicksort(list q)
list low, pivotList, hi
if length(q) ≤ 1
return q
select a pivot value from q
for each x in q except the pivot element
if x < pivot then add x to low
if x ≥ pivot then add x to high
add pivot to pivotList
return concatenate(quicksort(less), pivotList,
quicksort(greater))
EXECUTION
Execution

More Related Content

PPT
Sorting algorithms
PPTX
Data Structures - Lecture 8 [Sorting Algorithms]
PDF
L 14-ct1120
PPTX
Searching and sorting Techniques in Data structures
PPTX
Unit 7 sorting
PPTX
Unit vii sorting
PPT
Sorting algos
PPT
Sorting algorithms
Sorting algorithms
Data Structures - Lecture 8 [Sorting Algorithms]
L 14-ct1120
Searching and sorting Techniques in Data structures
Unit 7 sorting
Unit vii sorting
Sorting algos
Sorting algorithms

Similar to Sorting Data structure And Algorithm.pptx (20)

PPT
Sorting algorithms
PPT
Sorting algorithums > Data Structures & Algorithums
PPT
358 33 powerpoint-slides_14-sorting_chapter-14
PPT
Sorting algorithm
PPTX
UNIT I- Sesgfnbghnghnghmghmhgmhmhsion 4.pptx
PPTX
Sorting method data structure
PPT
Sorting algorithms v01
PPT
search_sort_v1.pptgghghhhggggjjjjjjllllllllvbbbbbcfdsdfffg
PPT
search_sort search_sortsearch_sort search_sortsearch_sortsearch_sortsearch_sort
PPT
search_sort Search sortSearch sortSearch sortSearch sort
PPT
Lecture_4 (Sorting Algorithms) before mids - Copy.ppt
PPTX
Chapter-2.pptx
PPT
Data Structure (MC501)
PPTX
Data structure.pptx
PDF
Lecture12,13,14.pdf
PPT
Sorting
ODP
Sorting Algorithm
PPT
ds 3Sorting.ppt
PPTX
Different Sorting tecniques in Data Structure
PPT
COMPUTER PROGRAMMING UNIT 1 Lecture 5
Sorting algorithms
Sorting algorithums > Data Structures & Algorithums
358 33 powerpoint-slides_14-sorting_chapter-14
Sorting algorithm
UNIT I- Sesgfnbghnghnghmghmhgmhmhsion 4.pptx
Sorting method data structure
Sorting algorithms v01
search_sort_v1.pptgghghhhggggjjjjjjllllllllvbbbbbcfdsdfffg
search_sort search_sortsearch_sort search_sortsearch_sortsearch_sortsearch_sort
search_sort Search sortSearch sortSearch sortSearch sort
Lecture_4 (Sorting Algorithms) before mids - Copy.ppt
Chapter-2.pptx
Data Structure (MC501)
Data structure.pptx
Lecture12,13,14.pdf
Sorting
Sorting Algorithm
ds 3Sorting.ppt
Different Sorting tecniques in Data Structure
COMPUTER PROGRAMMING UNIT 1 Lecture 5
Ad

Recently uploaded (20)

PPTX
Introduction to Artificial Intelligence
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
System and Network Administraation Chapter 3
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PPTX
ai tools demonstartion for schools and inter college
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PPTX
L1 - Introduction to python Backend.pptx
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Introduction to Artificial Intelligence
Upgrade and Innovation Strategies for SAP ERP Customers
System and Network Administraation Chapter 3
Reimagine Home Health with the Power of Agentic AI​
Design an Analysis of Algorithms II-SECS-1021-03
ai tools demonstartion for schools and inter college
CHAPTER 2 - PM Management and IT Context
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PTS Company Brochure 2025 (1).pdf.......
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Design an Analysis of Algorithms I-SECS-1021-03
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Navsoft: AI-Powered Business Solutions & Custom Software Development
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
L1 - Introduction to python Backend.pptx
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
How to Choose the Right IT Partner for Your Business in Malaysia
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Ad

Sorting Data structure And Algorithm.pptx

  • 2. SORTING  A process that organizes a collection of data into either ascending or descending order.  Can be used as a first step for searching the data.  Binary Search required a sorted array.
  • 3. SORTING ALGORITHMS  Selection Sort  Insertion Sort  Bubble Sort  Quick Sort  Merge Sort  Heap Sort
  • 4. SELECTION SORT  It is simple and easy to implement  It is inefficient for large list, usually used to sort lists of no more than 1000 items  In array of n elements, n-1 iterations are required to sort the array
  • 5. SELECTION SORT  Select the smallest value from the list.  Bring it to the first location of the list.  Find the next value and repeat the process by swapping the locations.
  • 6. SELECTION SORT  Suppose the name of the array is A and it has four elements with the following values: 4 19 1 3  To sort this array in ascending order, n-1, i.e. three iterations will be required.
  • 7. SELECTION SORT  Iteration-1 The array is scanned starting from the first to the last element and the element that has the smallest value is selected. The smallest value is 1 at location 3. The address of element that has the smallest value is noted and the selected value is interchanged with the first element i.e. A[1] and A[3] are swapped 4 19 1 3 1 19 4 3
  • 8. SELECTION SORT  Iteration-2 The array is scanned starting from the second to the last element and the element that has the smallest value is selected. The smallest value is 3 at location 4. The address of element that has the smallest value is noted. The selected value is interchanged with the second element i.e. A[2] and A[4] are swapped 1 19 4 3 1 3 4 19
  • 9. SELECTION SORT  Iteration-3 The array is scanned starting from the third to the last element and the element that has the smallest value is selected. The smallest value is 4 at location 3. The address of element that has the smallest value is noted. The selected value is interchanged with the third element i.e. A[3] and A[3] are swapped 1 3 4 19 1 3 4 19
  • 10. ANOTHER EXAMPLE: SELECTION SORT  26 33 43 100 46 88 52 17 53 77  17 | 33 43 100 46 88 52 26 53 77  17 26 | 43 100 46 88 52 33 53 77  17 26 33 | 100 46 88 52 43 53 77  17 26 33 43 | 46 88 52 100 53 77  17 26 33 43 46 | 88 52 100 53 77  17 26 33 43 46 52 | 88 100 53 77  17 26 33 43 46 52 53 | 100 88 77  17 26 33 43 46 52 53 77 | 88 100  17 26 33 43 46 52 53 77 88 | 100
  • 11. SELECTION SORT void selectionSort(int numbers[ ], int array_size) { int i, j; int min, temp; for (i = 0; i < array_size-1; i++) { min = i; for (j = i+1; j < array_size; j++) { if (numbers[j] < numbers[min]) min = j; } temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } }
  • 12. BUBBLE SORT  It is the oldest and simplest method and can be easily implemented  It is also the slowest and considered to be the most inefficient sorting algorithm  It works by comparing each item in the list with the item next to it, and swapping them if required  It is used only a small amount of data
  • 13. BUBBLE SORT 1. First Iteration 1. Starting from the first index, compare the first and the second elements. 2. If the first element is greater than the second element, they are swapped. 3. Now, compare the second and the third elements. Swap them if they are not in order. 4. The above process goes on until the last element. 2. The same process goes on for the remaining iterations.
  • 14. BUBBLE SORT  To sort data in an array of n elements, n-1 iterations are required  Following steps explain sorting of data in an array in acceding order  In first iteration, the largest value moves to the last position in the array  In second iteration, the above process is repeated and the second largest value moves to the second last position in the array and so on  In n-1 iteration, the data is arranged in ascending order
  • 15. BUBBLE SORT  Suppose the name of the array is A and it has four elements with the following values: 4 19 1 3  To sort this array in ascending order, n-1, i.e. three iterations will be required.
  • 16. BUBBLE SORT  Iteration-1  A[1] is compared with element A[2]. Since 4 is not greater than 19. there will be no change in the list.  A[2] is compared with element A[3]. Since 19 is greater than 1, the value are interchanged  A[3] is compared with element A[4]. Since 19 is grater than 3, the value are interchanged  Thus at the end of the first iteration, the largest value moves to the last position in the array 4 19 1 3 4 1 19 3 4 1 3 19
  • 17. BUBBLE SORT  Iteration-2  A[1] is compared with element A[2]. Since 4 is greater than 1, the value are interchanged  A[2] is compared with element A[3]. Since 4 is grater than 3, the value are interchanged  Thus at the end of the second iteration, the second largest value moves to the second last position in the array 1 4 3 19 1 3 4 19
  • 18. BUBBLE SORT  Iteration-3  A[1] is compared with element A[2]. Since 1 is not greater than 3, the value are not interchanged  So array is sorted in ascending order 1 3 4 19
  • 19. BUBBLE SORT  Bubble sort is similar to selection sort in the sense that it repeatedly finds the largest/smallest value in the unprocessed portion of the array and puts it back.  However, finding the largest value is not done by selection this time.  We "bubble" up the largest value instead.
  • 20. BUBBLE SORT  Compares adjacent items and exchanges them if they are out of order.  Comprises of several passes.  In one pass, the largest value has been “bubbled” to its proper position.  In second pass, the last value does not need to be compared.
  • 21. BUBBLE SORT void bubbleSort (int a[ ], int n) { int i, j, temp; for(int i = 0; i < n-1; i++) { for(int j=0; j < (n-1)-i; j++) { if(a[j] > a[j+1]) { temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } } } }
  • 22. BUBBLE SORT EXAMPLE 9, 6, 2, 12, 11, 9, 3, 7 6, 9, 2, 12, 11, 9, 3, 7 6, 2, 9, 12, 11, 9, 3, 7 6, 2, 9, 12, 11, 9, 3, 7 6, 2, 9, 11, 12, 9, 3, 7 6, 2, 9, 11, 9, 12, 3, 7 6, 2, 9, 11, 9, 3, 12, 7 6, 2, 9, 11, 9, 3, 7, 12
  • 23. BUBBLE SORT EXAMPLE 6, 2, 9, 11, 9, 3, 7, 12 2, 6, 9, 11, 9, 3, 7, 12 2, 6, 9, 9, 11, 3, 7, 12 2, 6, 9, 9, 3, 11, 7, 12 2, 6, 9, 9, 3, 7, 11, 12 6, 2, 9, 11, 9, 3, 7, 12 Notice that this time we do not have to compare the last two numbers as we know the 12 is in position. This pass therefore only requires 6 comparisons. First Pass Second Pass
  • 24. BUBBLE SORT EXAMPLE 2, 6, 9, 9, 3, 7, 11, 12 2, 6, 9, 3, 9, 7, 11, 12 2, 6, 9, 3, 7, 9, 11, 12 6, 2, 9, 11, 9, 3, 7, 12 2, 6, 9, 9, 3, 7, 11, 12 Second Pass First Pass Third Pass This time the 11 and 12 are in position. This pass therefore only requires 5 comparisons.
  • 25. BUBBLE SORT EXAMPLE 2, 6, 9, 3, 7, 9, 11, 12 2, 6, 3, 9, 7, 9, 11, 12 2, 6, 3, 7, 9, 9, 11, 12 6, 2, 9, 11, 9, 3, 7, 12 2, 6, 9, 9, 3, 7, 11, 12 Second Pass First Pass Third Pass Each pass requires fewer comparisons. This time only 4 are needed. 2, 6, 9, 3, 7, 9, 11, 12 Fourth Pass
  • 26. BUBBLE SORT EXAMPLE 2, 6, 3, 7, 9, 9, 11, 12 2, 3, 6, 7, 9, 9, 11, 12 6, 2, 9, 11, 9, 3, 7, 12 2, 6, 9, 9, 3, 7, 11, 12 Second Pass First Pass Third Pass The list is now sorted but the algorithm does not know this until it completes a pass with no exchanges. 2, 6, 9, 3, 7, 9, 11, 12 Fourth Pass 2, 6, 3, 7, 9, 9, 11, 12 Fifth Pass
  • 27. INSERTION SORT  It is simple as the bubble sort but it is almost twice as efficient as the bubble sort  It is relatively simple and easy to implement  It is inefficient for large lists
  • 28. INSERTION SORT  In insertion sorting, the list or array is scanned from the beginning to the end  In each iteration, one element is inserted into its correct position relative to the previously sorted elements of the list  The array elements are not swapped or interchanged  They are shifted towards the right of the list to make room for the new element to be inserted
  • 29. INSERTION SORT  Given an unsorted list.  Partition the list into two regions: sorted & unsorted.  At each step, take the first item from unsorted and place it into its correct position.  Also requires to shift the remaining items to make a room for the inserted item.
  • 30. INSERTION SORT  Suppose the name of the array is A and it has six elements with the following values: 16 17 2 8 18 1  To sort this array in ascending order, six iterations will be required.
  • 31. INSERTION SORT  Iteration-1 A[1] is compared with itself and it is not shifted. The array A remains the same 16 17 2 8 18 1 16 17 2 8 18 1
  • 32. INSERTION SORT  Iteration-2 All data of elements on left of A[2] that are greater than A[2] are shifted one position to the right to make room for A[2] to insert its data into the correct location. There is only one element with value 16 to the left of A[2]. Thus no shifting takes place because 16 is less than 17. So A[1] and A[2] are in correct position relative to each other. The array A remains same 16 17 2 8 18 1 16 17 2 8 18 1
  • 33. INSERTION SORT  Iteration-3 All data of elements on left of A[3] that are greater than A[3] are shifted one position to the right to make room for A[3] to insert its data into the correct location. There is two elements of left side of A[3] and both are greater than A[3]. Thus shift data A[1] & A[2] one position to right and insert the value of A[3] at A[1]. The array A after shifting and inserting value is: 16 17 2 8 18 1 2 16 17 8 18 1
  • 34. INSERTION SORT  Iteration-4 All data of elements on left of A[4] that are greater than A[4] are shifted one position to the right to make room for A[4] to insert its data into the correct location. There is three elements of left side of A[4] and A[2] & A[3] are greater than A[4]. Thus shift data A[2] & A[3] one position to right and insert the value of A[4] at A[2]. The array A after shifting and inserting value is: 2 16 17 8 18 1 2 8 16 17 18 1
  • 35. INSERTION SORT  Iteration-5 All data of elements on left of A[5] that are greater than A[5] are shifted one position to the right to make room for A[5] to insert its data into the correct location. There is four elements of left side of A[5] and all are less than A[5]. Thus no shifting & insertion takes place. The array A remains same: 2 8 16 17 18 1 2 8 16 17 18 1
  • 36. INSERTION SORT  Iteration-6 All data of elements on left of A[6] that are greater than A[6] are shifted one position to the right to make room for A[6] to insert its data into the correct location. There is five elements of left side of A[6] and all are greater than A[6]. Thus shift data of each element from A[1] to A[5] one position to right and insert the value of A[6] at A[1]. The array A after shifting and inserting value is: 2 8 16 17 18 1 1 2 8 16 17 18
  • 37. ALGORITHM – INSERTION SORT InsertionSort() Algorithm to sort an array A consisting of N elements in ascending order 1. Start 2. Repeat step 3 to 8 For C = 2 to N 3. Set Temp = A[C] 4. Set L = C 5. Repeat Step 6 to 7 While (L>1 and Temp<=A[L-1]) 6. Set A[L] = A[L-1] 7. L = L – 1 8. Set A[L] = Temp 9. Exit
  • 38. INSERTION SORT CONT….. • The insertion sort algorithm sorts the list by moving each element to its proper place Figure 6: Array list to be sorted Figure 7: Sorted and unsorted portions of the array list
  • 39. INSERTION SORT ALGORITHM (CONT’D) Figure 8: Move list[4] into list[2] Figure 9: Copy list[4] into temp
  • 40. INSERTION SORT ALGORITHM (CONT’D) Figure 10: Array list before copying list[3] into list[4], then list[2] into list[3] Figure 11: Array list after copying list[3] into list[4], and then list[2] into list[3]
  • 41. INSERTION SORT ALGORITHM (CONT’D) Figure 12: Array list after copying temp into list[2]
  • 42. AN EXAMPLE: INSERTION SORT InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 30 10 40 20 1 2 3 4 i =  j =  key =  A[j] =  A[j+1] = 
  • 43. InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 30 10 40 20 1 2 3 4 i = 2 j = 1 key = 10 A[j] = 30 A[j+1] = 10
  • 44. InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 30 30 40 20 1 2 3 4 i = 2 j = 1 key = 10 A[j] = 30 A[j+1] = 30
  • 45. InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 30 30 40 20 1 2 3 4 i = 2 j = 1 key = 10 A[j] = 30 A[j+1] = 30
  • 46. InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 30 30 40 20 1 2 3 4 i = 2 j = 0 key = 10 A[j] =  A[j+1] = 30
  • 47. InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 30 30 40 20 1 2 3 4 i = 2 j = 0 key = 10 A[j] =  A[j+1] = 30
  • 48. InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 30 40 20 1 2 3 4 i = 2 j = 0 key = 10 A[j] =  A[j+1] = 10
  • 49. InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 30 40 20 1 2 3 4 i = 3 j = 0 key = 10 A[j] =  A[j+1] = 10
  • 50. InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 30 40 20 1 2 3 4 i = 3 j = 0 key = 40 A[j] =  A[j+1] = 10
  • 51. InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 30 40 20 1 2 3 4 i = 3 j = 0 key = 40 A[j] =  A[j+1] = 10
  • 52. InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 30 40 20 1 2 3 4 i = 3 j = 2 key = 40 A[j] = 30 A[j+1] = 40
  • 53. InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 30 40 20 1 2 3 4 i = 3 j = 2 key = 40 A[j] = 30 A[j+1] = 40
  • 54. InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 30 40 20 1 2 3 4 i = 3 j = 2 key = 40 A[j] = 30 A[j+1] = 40
  • 55. InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 30 40 20 1 2 3 4 i = 4 j = 2 key = 40 A[j] = 30 A[j+1] = 40
  • 56. InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 30 40 20 1 2 3 4 i = 4 j = 2 key = 20 A[j] = 30 A[j+1] = 40
  • 57. InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 30 40 20 1 2 3 4 i = 4 j = 2 key = 20 A[j] = 30 A[j+1] = 40
  • 58. InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 30 40 20 1 2 3 4 i = 4 j = 3 key = 20 A[j] = 40 A[j+1] = 20
  • 59. InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 30 40 20 1 2 3 4 i = 4 j = 3 key = 20 A[j] = 40 A[j+1] = 20
  • 60. InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 30 40 40 1 2 3 4 i = 4 j = 3 key = 20 A[j] = 40 A[j+1] = 40
  • 61. InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 30 40 40 1 2 3 4 i = 4 j = 3 key = 20 A[j] = 40 A[j+1] = 40
  • 62. InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 30 40 40 1 2 3 4 i = 4 j = 3 key = 20 A[j] = 40 A[j+1] = 40
  • 63. InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 30 40 40 1 2 3 4 i = 4 j = 2 key = 20 A[j] = 30 A[j+1] = 40
  • 64. InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 30 40 40 1 2 3 4 i = 4 j = 2 key = 20 A[j] = 30 A[j+1] = 40
  • 65. InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 30 30 40 1 2 3 4 i = 4 j = 2 key = 20 A[j] = 30 A[j+1] = 30
  • 66. InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 30 30 40 1 2 3 4 i = 4 j = 2 key = 20 A[j] = 30 A[j+1] = 30
  • 67. InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 30 30 40 1 2 3 4 i = 4 j = 1 key = 20 A[j] = 10 A[j+1] = 30
  • 68. InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 30 30 40 1 2 3 4 i = 4 j = 1 key = 20 A[j] = 10 A[j+1] = 30
  • 69. InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 20 30 40 1 2 3 4 i = 4 j = 1 key = 20 A[j] = 10 A[j+1] = 20
  • 70. InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } 10 20 30 40 1 2 3 4 i = 4 j = 1 key = 20 A[j] = 10 A[j+1] = 20 Done!
  • 71. EXAMPLE: INSERTION SORT  99 | 55 4 66 28 31 36 52 38 72  55 99 | 4 66 28 31 36 52 38 72  4 55 99 | 66 28 31 36 52 38 72  4 55 66 99 | 28 31 36 52 38 72  4 28 55 66 99 | 31 36 52 38 72  4 28 31 55 66 99 | 36 52 38 72  4 28 31 36 55 66 99 | 52 38 72  4 28 31 36 52 55 66 99 | 38 72  4 28 31 36 38 52 55 66 99 | 72  4 28 31 36 38 52 55 66 72 99 |
  • 72. INSERTION SORT ALGORITHM void insertionSort(int array[], int length) { int i, j, value; for(i = 1; i < length; i++) { value = a[i]; for (j = i - 1; j >= 0 && a[ j ] > value; j--) { a[j + 1] = a[ j ]; } a[j + 1] = value; } }
  • 73. MERGE SORT  Merge sort is a sorting algorithm for rearranging lists (or any other data structure that can only be accessed sequentially) into a specified order.  It is a particularly good example of the divide and conquer algorithmic paradigm.
  • 74. MERGE SORT  Conceptually, merge sort works as follows: Divide the unsorted list into two sub-lists of about half the size. Sort each of the two sub-lists. Merge the two sorted sub-lists back into one sorted list.
  • 75. MERGE SORT Array mergeSort(Array m) Array left, right. if length(m) ≤ 1 return m else middle = length(m) / 2 for each x in m up to middle add x to left for each x in m after middle add x to right left = mergesort(left) right = mergesort(right) result = merge(left, right) return result
  • 76. MERGE SORT Array merge(left,right) Array result while length(left) > 0 and length(right) > 0 if first(left) ≤ first(right) append first(left) to result left = rest(left) else append first(right) to result right = rest(right) if length(left) > 0 append left to result if length(right) > 0 append right to result return result
  • 87. MERGE SORT ANOTHER EXAMPLE  Suppose the name of the array is AB and it has six elements with the following values: 16 17 2 8 18 1  To sort this array in ascending order
  • 88. MERGE SORT AB  Divide array AB into two sub-arrays A & B A B  Sort A & B using Bubble or selection or insertion sort A B 16 17 2 8 18 1 16 17 2 8 18 1 2 16 17 1 8 18
  • 89. MERGE SORT A B  Compare A[1] to B[1], so B[1] is less than A[1], the value of B[1] is move to AB[1] AB 1 2 16 17 1 8 18
  • 90. MERGE SORT A B  Compare A[1] to B[2], so A[1] is less than B[2], the value of A[2] is move to AB[2] AB 1 2 2 16 17 1 8 18
  • 91. MERGE SORT A B  Compare A[2] to B[2], so B[2] is less than A[2], the value of B[2] is move to AB[3] AB 1 2 8 2 16 17 1 8 18
  • 92. MERGE SORT A B  Compare A[2] to B[3], so A[2] is less than B[3], the value of A[2] is move to AB[4] AB 1 2 8 16 2 16 17 1 8 18
  • 93. MERGE SORT A B  Compare A[3] to B[3], so A[3] is less than B[3], the value of A[3] is move to AB[5] AB  At the end, B[3] is move to AB[6], array is sorted AB 1 2 8 16 17 2 16 17 1 8 18 1 2 8 16 17 18
  • 94. ALGORITHM Mergesort(Passed an array) if array size > 1 Divide array in half Call Mergesort on first half. Call Mergesort on second half. Merge two halves. Merge(Passed two arrays) Compare leading element in each array Select lower and place in new array. (If one input array is empty then place remainder of other array in output array) LB
  • 95. 67 45 23 14 6 33 98 42
  • 96. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42
  • 97. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98
  • 98. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98
  • 99. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 Merge
  • 100. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 23 Merge
  • 101. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 23 98 Merge
  • 102. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 23 98
  • 103. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 Merge 23 98
  • 104. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 14 Merge 23 98
  • 105. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 45 Merge 23 98 14
  • 106. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 Merge 98 45 14 23
  • 107. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 Merge 98 14 14 23 45
  • 108. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 Merge 23 14 14 23 98 45
  • 109. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 Merge 23 98 45 14 14 23 45
  • 110. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 Merge 23 98 45 14 14 23 45 98
  • 111. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 23 98 45 14 14 23 45 98
  • 112. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 23 98 45 14 14 23 45 98
  • 113. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 Merge 23 98 45 14 14 23 45 98
  • 114. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 6 Merge 23 98 45 14 14 23 45 98
  • 115. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 67 Merge 23 98 45 14 6 14 23 45 98
  • 116. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 23 98 45 14 67 6 14 23 45 98
  • 117. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 23 98 45 14 67 6 14 23 45 98
  • 118. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 33 23 98 45 14 67 6 14 23 45 98
  • 119. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 42 23 98 45 14 67 6 33 14 23 45 98
  • 120. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 23 98 45 14 67 6 42 33 14 23 45 98
  • 121. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 23 98 45 14 6 42 33 14 23 45 98 6 67
  • 122. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 23 98 45 14 6 33 14 23 45 98 6 33 67 42
  • 123. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 23 98 45 14 6 42 33 14 23 45 98 6 33 42 67
  • 124. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 23 98 45 14 67 6 42 33 14 23 45 98 6 33 42 67
  • 125. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 23 98 45 14 67 6 42 33 23 45 98 33 42 67 14 6
  • 126. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 23 98 45 14 67 6 42 33 23 45 98 6 42 67 6 14 33
  • 127. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 23 98 45 14 67 6 42 33 14 45 98 6 42 67 6 14 23 33
  • 128. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 23 98 45 14 67 6 42 33 14 23 98 6 42 67 6 14 23 45 33
  • 129. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 23 98 45 14 67 6 42 33 14 23 98 6 33 67 6 14 23 33 45 42
  • 130. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 23 98 45 14 67 6 42 33 14 23 98 6 33 42 6 14 23 33 42 45 67
  • 131. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 23 98 45 14 67 6 42 33 14 23 45 6 33 42 6 14 23 33 42 45 98 67
  • 132. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 23 98 45 14 67 6 42 33 14 23 45 98 6 33 42 67 6 14 23 33 42 45 67
  • 133. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 Merge 23 98 45 14 67 6 42 33 14 23 45 98 6 33 42 67 6 14 23 33 42 45 67 98
  • 134. 67 45 23 14 6 33 98 42 67 45 23 14 6 33 98 42 45 23 14 98 23 98 45 14 67 6 33 42 67 6 33 42 23 98 45 14 67 6 42 33 14 23 45 98 6 33 42 67 6 14 23 33 42 45 67 98
  • 135. 67 45 23 14 6 33 98 42 6 14 23 33 42 45 67 98
  • 136. QUICK SORT  Quick sort sorts by employing a divide and conquer strategy to divide a list into two sub-lists.  The steps are:  Pick an element, called a pivot, from the list.  Reorder the list so that all elements which are less than the pivot come before the pivot and so that all elements greater than the pivot come after it (equal values can go either way).  After this partitioning, the pivot is in its final position. This is called the partition operation.  Recursively sort the sub-list of lesser elements and the sub-list of greater elements.
  • 137. QUICK SORT  Choose the appropriate pivot, either randomly or near the median of the array elements.  Avoid a pivot which makes either of the two halves empty.
  • 138. EXAMPLE We are given array of n integers to sort: 40 20 10 80 60 50 7 30 100
  • 139. PICK PIVOT ELEMENT There are a number of ways to pick the pivot element. In this example, we will use the first element in the array: 40 20 10 80 60 50 7 30 100
  • 140. PARTITIONING ARRAY Given a pivot, partition the elements of the array such that the resulting array consists of: 1. One sub-array that contains elements >= pivot 2. Another sub-array that contains elements < pivot The sub-arrays are stored in the original data array. Partitioning loops through, swapping elements below/above pivot.
  • 141. 40 20 10 80 60 50 7 30 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] Too_big_index too_small_index
  • 142. 40 20 10 80 60 50 7 30 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index 1. While data[too_big_index] <= data[pivot] ++too_big_index
  • 143. 40 20 10 80 60 50 7 30 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index 1. While data[too_big_index] <= data[pivot] ++too_big_index
  • 144. 40 20 10 80 60 50 7 30 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index 1. While data[too_big_index] <= data[pivot] ++too_big_index
  • 145. 40 20 10 80 60 50 7 30 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index 1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index
  • 146. 40 20 10 80 60 50 7 30 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index 1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index
  • 147. 40 20 10 80 60 50 7 30 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index 1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index > too_small_index swap data[too_big_index] and data[too_small_index]
  • 148. 40 20 10 30 60 50 7 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index 1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index > too_small_index swap data[too_big_index] and data[too_small_index]
  • 149. 40 20 10 30 60 50 7 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index 1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index > too_small_index swap data[too_big_index] and data[too_small_index] 4. While too_small_index > too_big_index, go to 1.
  • 150. 40 20 10 30 60 50 7 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index 1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index > too_small_index swap data[too_big_index] and data[too_small_index] 4. While too_small_index > too_big_index, go to 1.
  • 151. 40 20 10 30 60 50 7 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index 1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index > too_small_index swap data[too_big_index] and data[too_small_index] 4. While too_small_index > too_big_index, go to 1.
  • 152. 40 20 10 30 60 50 7 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index 1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index > too_small_index swap data[too_big_index] and data[too_small_index] 4. While too_small_index > too_big_index, go to 1.
  • 153. 40 20 10 30 60 50 7 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index 1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index > too_small_index swap data[too_big_index] and data[too_small_index] 4. While too_small_index > too_big_index, go to 1.
  • 154. 40 20 10 30 60 50 7 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index 1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index > too_small_index swap data[too_big_index] and data[too_small_index] 4. While too_small_index > too_big_index, go to 1.
  • 155. 1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index > too_small_index swap data[too_big_index] and data[too_small_index] 4. While too_small_index > too_big_index, go to 1. 40 20 10 30 7 50 60 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index
  • 156. 1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index > too_small_index swap data[too_big_index] and data[too_small_index] 4. While too_small_index > too_big_index, go to 1. 40 20 10 30 7 50 60 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index
  • 157. 1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index > too_small_index swap data[too_big_index] and data[too_small_index] 4. While too_small_index > too_big_index, go to 1. 40 20 10 30 7 50 60 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index
  • 158. 1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index > too_small_index swap data[too_big_index] and data[too_small_index] 4. While too_small_index > too_big_index, go to 1. 40 20 10 30 7 50 60 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index
  • 159. 1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index > too_small_index swap data[too_big_index] and data[too_small_index] 4. While too_small_index > too_big_index, go to 1. 40 20 10 30 7 50 60 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index
  • 160. 1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index > too_small_index swap data[too_big_index] and data[too_small_index] 4. While too_small_index > too_big_index, go to 1. 40 20 10 30 7 50 60 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index
  • 161. 1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index > too_small_index swap data[too_big_index] and data[too_small_index] 4. While too_small_index > too_big_index, go to 1. 40 20 10 30 7 50 60 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index
  • 162. 1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index > too_small_index swap data[too_big_index] and data[too_small_index] 4. While too_small_index > too_big_index, go to 1. 40 20 10 30 7 50 60 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index
  • 163. 1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index > too_small_index swap data[too_big_index] and data[too_small_index] 4. While too_small_index > too_big_index, go to 1. 40 20 10 30 7 50 60 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index
  • 164. 1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index > too_small_index swap data[too_big_index] and data[too_small_index] 4. While too_small_index > too_big_index, go to 1. 5. Swap data[too_small_index] and data[pivot_index] 40 20 10 30 7 50 60 80 100 pivot_index = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index
  • 165. 1. While data[too_big_index] <= data[pivot] ++too_big_index 2. While data[too_small_index] > data[pivot] --too_small_index 3. If too_big_index > too_small_index swap data[too_big_index] and data[too_small_index] 4. While too_small_index > too_big_index, go to 1. 5. Swap data[too_small_index] and data[pivot_index] 7 20 10 30 40 50 60 80 100 pivot_index = 4 [0] [1] [2] [3] [4] [5] [6] [7] [8] too_big_index too_small_index
  • 166. PARTITION RESULT 7 20 10 30 40 50 60 80 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] <= data[pivot] > data[pivot]
  • 167. RECURSION: QUICKSORT SUB-ARRAYS 7 20 10 30 40 50 60 80 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] <= data[pivot] > data[pivot]
  • 168. QUICK SORT function quicksort(list q) list low, pivotList, hi if length(q) ≤ 1 return q select a pivot value from q for each x in q except the pivot element if x < pivot then add x to low if x ≥ pivot then add x to high add pivot to pivotList return concatenate(quicksort(less), pivotList, quicksort(greater))