0% found this document useful (0 votes)
7 views

Sort - Merge and Quick Sort

DSA NOTES DATA STRUCTURE AND ALGORITHMS

Uploaded by

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

Sort - Merge and Quick Sort

DSA NOTES DATA STRUCTURE AND ALGORITHMS

Uploaded by

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

Data Structures – CSC211

Sorting: Merge and Quick Sort


Merge Sort
 Idea:
 Take the array you would like to sort and divide it in half to
create 2 unsorted subarrays.
 Next, sort each of the 2 subarrays.
 Finally, merge the 2 sorted subarrays into 1 sorted array.
Merge Sort
Merge Sort
 Although the merge step produces a sorted array, we
have overlooked a very important step.
 How did we sort the 2 halves before performing
the merge step?

We used merge sort!


Merge Sort
 By continually calling the merge sort algorithm, we
eventually get a subarray of size 1.
 Since an array with only 1 element is clearly sorted, we
can back out and merge 2 arrays of size 1.
Merge Sort
Merge Sort

arrayA 1 13 24 26 We compare arrayA[indexA]


with arrayB[indexB].
indexA Whichever value is smaller is
placed into arrayC[indexC].

arrayB 2 15 27 38 1 < 2 so we insert


arrayA[indexA] into
indexB arrayC[indexC]

arrayC
indexC
Merge Sort

arrayA 1 13 24 26
2 < 13 so we insert
indexA arrayB[indexB] into
arrayC[indexC]

arrayB 2 15 27 38
indexB

arrayC 1
indexC
Merge Sort

arrayA 1 13 24 26
13 < 15 so we insert
indexA arrayA[indexA] into
arrayC[indexC]

arrayB 2 15 27 38
indexB

arrayC 1 2
indexC
Merge Sort

arrayA 1 13 24 26
15 < 24 so we insert
indexA arrayB[indexB] into
arrayC[indexC]

arrayB 2 15 27 38
indexB

arrayC 1 2 13
indexC
Merge Sort

arrayA 1 13 24 26
24 < 27 so we insert
indexA arrayA[indexA] into
arrayC[indexC]

arrayB 2 15 27 38
indexB

arrayC 1 2 13 15
indexC
Merge Sort

arrayA 1 13 24 26
26 < 27 so we insert
indexA arrayA[indexA] into
arrayC[indexC]

arrayB 2 15 27 38
indexB

arrayC 1 2 13 15 24
indexC
Merge Sort
arrayA 1 13 24 26
Since we have exhausted
one of the arrays, arrayA,
we simply copy the
remaining items from the
arrayB 2 15 27 38 other array, arrayB, into
arrayC
indexB

arrayC
1 2 13 15 24 26
indexC
Merge Sort

arrayA 1 13 24 26

arrayB 2 15 27 38

arrayC 1 2 13 15 24 26 27 38
Merge Sort Pseudocode
mergesort(list, first, last)
IF first < last
mid = (first + last)/2;
// Sort the 1st half of the list
mergesort(list, first, mid)
// Sort the 2nd half of the list
mergesort(list, mid+1, last)
// Merge the 2 sorted halves
merge(list, first, mid, last)
END IF
END mergesort
Merge Sort Pseudocode (cont)
// Merge 2 sorted halfs
merge(list, first, mid, last)
// Initialize the first and last indices of our subarrays
firstA = first
lastA = mid
firstB = mid+1
lastB = last

index = firstA // Index into our temp array


END merge
Merge Sort Pseudocode (cont)
// Start the merging
WHILE firstA <= lastA AND firstB <= lastB
IF list[firstA] < list[firstB]
tempArray[index] = list[firstA]
firstA = firstA + 1
ELSE
tempArray[index] = list[firstB]
firstB = firstB + 1
END IF
END WHILE
Merge Sort Pseudocode (cont)
// At this point, one of our subarrays is empty
// Now go through and copy any remaining items
// from the non-empty array into our temp array
WHILE firstA <= lastA
tempArray[index] = list[firstA]
firstA = firstA + 1
index = index + 1
END WHILE
WHILE firstB <= lastB
tempArray[index] = list[firstB]
firstB = firstB + 1
index = index + 1
END WHILE
Merge Sort Pseudocode (cont)
// Finally, we copy our temp array back into
// our original array
index = first
LOOP index <= last
list[index] = tempArray[index]
index = index + 1
END LOOP
Quick Sort
 In the bubble sort, consecutive items are compared
and possibly exchanged on each pass through the list.
 This means that many exchanges may be needed to
move an element to its correct position.
 Quick sort is more efficient than bubble sort because
a typical exchange involves elements that are far
apart, so fewer exchanges are required to correctly
position an element.
Quick Sort
 Each iteration of the quick sort selects an element,
known as the pivot, and divides the list into 3 groups:
 Elements whose keys are less than (or equal to) the pivot’s key.
 The pivot element
 Elements whose keys are greater than (or equal to) the pivot’s
key.

 The sorting then continues by quick sorting the left


partition followed by quick sorting the right partition.
 The basic algorithm is as follows:
Quick Sort
1) PartitioningStep: Take an element in the unsorted
array and determine its final location in the sorted
array. This occurs when all values to the left of the
element in the array are less than (or equal to) the
element, and all values to the right of the element are
greater than (or equal to) the element. We now have
1 element in its proper location and two unsorted
subarrays.
2) Recursive Step: Perform step 1 on each unsorted
subarray.
Quick Sort
 Each time step 1 is performed on a subarray, another
element is placed in its final location of the sorted array,
and two unsorted subarrays are created.
 When a subarray consists of one element, that subarray is
sorted.
 Therefore that element is in its final location.
Quick Sort
 There are several partitioning strategies used in
practice (i.e., several “versions” of quick sort), but the
one we are about to describe is known to work well.
 For simplicity we will choose the last element to be
the pivot element.
 We could also chose a different pivot element and
swap it with the last element in the array.
Quick Sort
 Below is the array we would like to sort:

1 4 8 9 0 11 5 10 7 6
 The index left starts at the first element and right
starts at the next-to-last element.
1 4 8 9 0 11 5 10 7 6
left right
 We want to move all the elements smaller than the
pivot to the left part of the array and all the elements
larger than the pivot to the right part.
Quick Sort
 We move left to the right, skipping over elements that are
smaller than the pivot.

1 4 8 9 0 11 5 10 7 6
left right
Quick Sort
 We then move right to the left, skipping over
elements that are greater than the pivot.

1 4 8 9 0 11 5 10 7 6
left right

 When left and right have stopped, left is on an element


greater than (or equal to) the pivot and right is on an
element smaller than (or equal to) the pivot.
Quick Sort
 If left is to the left of right (or if left = right), those elements
are swapped.
1 4 8 9 0 11 5 10 7 6
left right

1 4 5 9 0 11 8 10 7 6
left right
 The effect is to push a large element to the right and a small
element to the left.
 We then repeat the process until left and right cross.
Quick Sort

1 4 5 9 0 11 8 10 7 6
left right

1 4 5 9 0 11 8 10 7 6
left right

1 4 5 0 9 11 8 10 7 6
left right
Quick Sort

1 4 5 0 9 11 8 10 7 6
left right

1 4 5 0 9 11 8 10 7 6
right left
Quick Sort
1 4 5 0 9 11 8 10 7 6
right left
 At this point, left and right have crossed so no swap is
performed.
 The final part of the partitioning is to swap the pivot
element with left.

1 4 5 0 6 11 8 10 7 9
right left
Quick Sort
 Note that all elements to the left of the pivot are less
than (or equal to) the pivot and all elements to the right
of the pivot are greater than (or equal to) the pivot.
 Hence, the pivot element has been placed in its final
sorted position.

1 4 5 0 6 11 8 10 7 9
right left
Quick Sort
 We now repeat the process using the sub-arrays to the
left and right of the pivot.

1 4 5 0 6 11 8 10 7 9

1 4 5 0 11 8 10 7 9
Quick Sort
 A couple of notes about quick sort:
 There are more optimal ways to choose the pivot value (such
as the median-of-three method).
 Also, when the subarrays get small, it becomes more efficient
to use the insertion sort as opposed to continued use of quick
sort.
References
 www.linux.wku.edu
 www.vu.edu.pk
 M.Azam, COMSATS, ABBOTTABAD

You might also like