100% found this document useful (1 vote)
23 views23 pages

Quick N Merge

Quick sort and merge sort lec notes for GGSIPU BTech 3rd semester subject DSA.

Uploaded by

Khushal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
23 views23 pages

Quick N Merge

Quick sort and merge sort lec notes for GGSIPU BTech 3rd semester subject DSA.

Uploaded by

Khushal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

QUICK SORT AND MERGE SORT

Divide and Conquer


Recursive in structure
•Divide the problem into sub-problems that are similar
to the original but smaller in size

•Conquer the sub-problems by solving them recursively.


If they are small enough, just solve them in a
straightforward manner.

•Combine the solutions to create a solution to the


original problem
Merge Sort
• Merge sort algorithms focuses on two main concepts to improve its
performance (running time):
• A smaller list takes few steps and thus less time to sort than a large
list.
• Less steps, thus less time is needed to create a sorted list from two
sorted lists rather than creating it using two unsorted lists.

The basic steps of a merge sort algorithm are as follows:


• If the array is of length 0 or 1, then it is already sorted. Otherwise:
• (Conceptually) divide the unsorted array into two sub- arrays of
about half the size.
• Use merge sort algorithm recursively to sort each sub-array
• Merge the two sub-arrays to form a single sorted list
Merge Sort
Sorting Problem: Sort a sequence of n elements into non-
decreasing order.

Divide: Divide the n-element sequence to be sorted into two


subsequences of n/2 elements each
Conquer: Sort the two subsequences recursively using merge
sort.
Combine: Merge the two sorted subsequences to produce the
sorted answer.
Merge Sort
Example: Sort the array given below using merge sort
39 9 81 45 90 27 72 18

Divide and conquer the array


39 9 81 45 90 27 72 18

39 9 81 45 90 27 72 18

39 9 81 45 90 27 72 18

90 27 72 18
39 9 81 45

72
39 9 81 45 90 27 18

9 39 45 81 27 90 18 72

9 39 45 81 18 27 72 90

9 18 27 39 45 72 81 90

Combine the elements to form a sorted array


Merge Sort
• To understand the merge algorithm, consider figure which shows
how we merge two lists to form one list. For the sake of
understanding we have taken two sub-lists each containing four
elements. The same concept can be utilized to merge 4 sub-lists
containing two elements, and eight sub-lists having just one
element.
9 39 45 81 18 27 72 90

BEG, I MID J END

TEMP

INDEX

9 39 45 81 18 27 72 90

BEG I mID J END


Merge Sort
TEMP

9 18

INDEX
9 39 45 81 18 27 72 90
BEG I Mid J END
TEMP

9 18 27

INDEX

9 39 45 81 18 27 72 90
BEG I Mid J END
TEMP

9 18 27 39

INDEX
Merge Sort
9 39 45 81 18 27 72 90

BEG I Mid J END

9 18 27 39 45

INDEX

9 39 45 81 18 27 72 90

BEG I,Mid J END

9 18 27 39 45 72

INDEX

When I is greater than MID copy the remaining elements of the right sub-array in TEMP

9 18 27 39 45 72 72 81 90
INDEX
Merge Sort
MERGE (ARR, BEG, MID, END)

Step 1: [Initialize] SET I = BEG, J = MID + 1, INDEX = 0


Step 2: Repeat while (I <= MID) AND (J<=END)
IF ARR[I] < ARR[J], then
SET TEMP[INDEX] = ARR[I]
SET I = I + 1
ELSE
SET TEMP[INDEX] = ARR[J]
SET J = J + 1
[END OF IF]
SET INDEX = INDEX + 1
[END OF LOOP]
Step 3: [ Copy the remaining elements of right sub-array, if any] IF I > MID, then
Repeat while J <= END
SET TEMP[INDEX] = ARR[J]
SET INDEX = INDEX + 1, SET J = J + 1
[END OF LOOP]
[Copy the remaining elements of left sub-array, if any] Else
Repeat while I <= MID
SET TEMP[INDEX] = ARR[I]
SET INDEX = INDEX + 1, SET I = I + 1
[END OF LOOP]
[END OF IF]
Step 4: [Copy the contents of TEMP back to ARR] SET K=0
Step 5: Repeat while K < INDEX
a. SET ARR[K] = TEMP[K]
b. SET K = K + 1
[END OF LOOP]
Step 6: END
Merge Sort

MERGE_SORT( ARR, BEG, END)

Step 1: IF BEG < END, then


SET MID = (BEG + END)/2
CALL MERGE_SORT( ARR, BEG, MID)
CALL MERGE_SORT (ARR, MID + 1, END)
MERGE (ARR, BEG, MID, END)
[END OF IF]
Step 2: END
Complexity of Merge Sort Algorithm
• The running time of the merge sort algorithm in average case and worst
case can be given as O(n logn). Although algorithm merge sort has an
optimal time complexity but a major drawback of this algorithm is that
it needs an additional space of O(n) for the temporary array TEMP
Quick Sort
• Quicksort is a widely used sorting algorithm developed by C. A. R. Hoare that
makes O(nlogn) comparisons in average case to sort an array of n elements.
However, in the worst case, quick sort algorithm has quadratic running time
given as O(n2). Basically, the quick sort algorithm is faster than other O(nlogn)
algorithms, because efficient implementation of the algorithm can minimize the
probability of requiring quadratic time.
The quick sort algorithm works as follows:
• Select an element pivot from the array elements
• Re-arrange the elements in the array in such a way that all elements that are
less than the pivot appear before the pivot and all elements greater than the
pivot element come after it (equal values can go either way). After such a
partitioning, the pivot is placed in its final position. This is called the partition
operation.
• Recursively sort the two sub-arrays thus obtained. (One with sub-list of lesser
value than that of the pivot element and the other having higher value
elements).
Quick Sort
• The main task in the quick sort algorithm is to find the pivot element, which will
partition the array into two halves. To understand how we find the pivot element
follow the steps given below. (we will take the first element in the array as pivot)

• Set the index of the first element in the array to loc and left variables. Also set
the index of the last element of the array to the right variable.

• That is, loc =0, left = 0 and right = n-1 (where n in the number of elements in the
array)

• Start from the element pointed by right and scan the array from right to left,
comparing each element on the way with the element pointed by variable loc.

• That is, a[loc]should be less than a[right]. If that is the case then simply continue
comparing until right becomes equal to loc. Because once right = loc, then it
means the pivot has been placed in its correct position.
Quick Sort
• However, if at any point we have a[loc]>a[right] then, interchange the two
values and jump to step 3.

• Set loc = right


• Start from the element pointed by left and scan the array from left to right,
comparing each element on the way with the element pointed by variable loc.

• That is, a[loc]should be greater than a[left].

• If that is the case then simply continue comparing until left becomes equal
to loc. Because once left = loc, then it means the pivot has been placed in
its correct position.

• However, if at any point we have a[loc]<a[left] then, interchange the two


values and jump to step 2
• Set loc = left.
Quick Sort
Quick Sort
Quick Sort
Example: Consider the array given below and sort the elements using quick sort
algorithm.
27 10 36 18 25 45

We choose the first element as the pivot. Set loc = 0, left = 0,


right = 5 as,
27 10 36 18 25 45

Loc Right
Left

Scan from right to left. Since a[loc] < a[right], decrease the value of right.
27 10 36 18 25 45

Loc Right
Left
Quick Sort

Since, a[loc] > a[right], interchange the two values and set loc = right.

25 10 36 18 27 45

Left Right, Loc

25 10 36 18 27 45

Left
Right, Loc
Quick Sort
Start scanning from left to right. Since, a[loc] > a[right], increment
the value of left.

25 10 36 18 27 45

Left
Right, Loc

Now since, a[loc] < a[right], interchange the values and set loc = left
25 10 27 18 36 45

Left, Loc
Right,
Quick Sort
Scan from right to left. Since a[loc] < a[right], decrement the value of
right.
25 10 27 18 36 45

Left, Loc Right,

Since, a[loc] > a[right], interchange the two values and set loc =
right. 25 10 18 27 36 45

Lef Right, Loc


t

Start scanning from left to right. Since, a[loc] > a[right], increment
the value of left. 25 10 18 27 36 45

Left, Right, Loc


Quick Sort
PARTITION ( ARR, BEG, END, LOC)

Step 1: [Initialize] SET LEFT = BEG, RIGHT = END, LOC = BEG, FLAG = 0
Step 2: Repeat Steps 3 to while FLAG = 0
Step 3: Repeat while ARR[LOC] <= ARR[RIGHT] AND LOC != RIGHT
SET RIGHT = RIGHT – 1
[END OF LOOP]
Step 4: IF LOC == RIGHT, then
SET FLAG = 1
ELSE IF ARR[LOC] > ARR[RIGHT], then
SWAP ARR[LOC] with ARR[RIGHT]
SET LOC = RIGHT
[END OF IF]
Step 5: IF FLAG = 0, then
Repeat while ARR[LOC] >= ARR[LEFT] AND LOC != LEFT
SET LEFT = LEFT + 1
[END OF LOOP]
Step 6: IF LOC == LEFT, then
SET FLAG = 1
ELSE IF ARR[LOC] < ARR[LEFT], then
SWAP ARR[LOC] with ARR[LEFT]
SET LOC = LEFT
[END OF IF]
[END OF IF]
Step 7: [END OF LOOP]
Step 8: END
Quick Sort

QUICK_SORT ( ARR, BEG, END)

Step 1: IF (BEG < END), then


CALL PARTITION ( ARR, BEG, END, LOC)
CALL QUICKSORT(ARR, BEG, LOC – 1)
CALL QUICKSORT(ARR, LOC + 1, END)
[END OF IF]
Step 2: END
Complexity of Quick Sort Algorithm
• In the average case, the running time of the quick sort algorithm can be given
as, O(nlogn). The partitioning of the array which simply loops over the elements
of the array once, uses O(n) time.
• In the best case, every time we partition the array, we divide the list into two
nearly equal pieces. That is, recursive call processes a sub-array of half the size.
Since, at the most only logn nested calls can be made before we reach a sub-
array of size 1. This means that the depth of the call tree is O(logn). And
because at each level there can only be O(n), the resultant time is given as
O(nlogn) time.
• Practically, the efficiency of the quick sort algorithm very much depends on the
element is chosen as the pivot. The worst-case efficiency of the quick sort is
given as O(n2). The worst case occurs when the array is already sorted (either in
ascending or descending order) and the left-most element is chosen as the
pivot.
• However, many implementations randomly choose the pivot element. The
randomized version of the quick sort algorithm always has an algorithmic
complexity of O(n log n).

You might also like