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

SORTING(Merge, Quick and Insertion)

The document describes three sorting algorithms: Merge Sort, Quick Sort, and Insertion Sort. Merge Sort uses a divide and conquer approach to sort an array by dividing it into halves and merging sorted halves, while Quick Sort reduces the sorting problem by partitioning the array around a pivot. Insertion Sort builds a sorted array one element at a time by inserting each new element into its correct position within the already sorted portion of the array.

Uploaded by

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

SORTING(Merge, Quick and Insertion)

The document describes three sorting algorithms: Merge Sort, Quick Sort, and Insertion Sort. Merge Sort uses a divide and conquer approach to sort an array by dividing it into halves and merging sorted halves, while Quick Sort reduces the sorting problem by partitioning the array around a pivot. Insertion Sort builds a sorted array one element at a time by inserting each new element into its correct position within the already sorted portion of the array.

Uploaded by

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

MERGE SORT

Merge Sort is a Divide and Conquer algorithm. It divides input array in two halves, calls itself
for the two halves and then merges the two sorted halves. The merge() function is used for
merging two halves. The merge(arr, l, m, r) is key process that assumes that arr[l..m] and
arr[m+1..r] are sorted and merges the two sorted sub-arrays into one.
5. QUICK SORT
Quick sort is an algorithm of divide and conquer type. That is, the problem of sorting a
set is reduced to the problem of sorting two smaller sets. We illustrate this “Reduction
step” by means of a specific example.
Suppose A is a following list of 12 numbers

44 33 11 55 70 90 40 60 99 22 88 66

The reduction step of the quick sort algorithm finds the final position of one of the
numbers; in this illustration, we use the first number 44. This is accomplished as follows.
Beginning with the last number 66, scan the list from right to left, comparing each
number with 44 and stopping at the first number less than 44. The number is 22.
Interchange 44 and 22 to obtain the list.

22 33 11 55 70 90 40 60 99 44 88 66

(Observe that the number 88 and 66 to the right of 44 are each greater than 44).
Beginning with 22, next scan the list in the opposite direction from left to right,
comparing each number with 44 and stopping at the first number greater than 44. The
number is 55. Interchange 44 and 55 to obtain the list.

22 33 11 44 70 90 40 60 99 55 88 66

(Observe that the number 22, 33 and 11 to the left of 44 are each less than 44). Beginning
with 55, now scan the list in the original direction from right to left, comparing each
number with 44 and stopping at the first number less than 44. The number is 40.
Interchange 44 and 40 to obtain the list.

22 33 11 40 70 90 44 60 99 55 88 66

(Observe that the numbers to the right of 44 are greater than 44). Beginning with 40, next
scan the list in the opposite direction from left to right, comparing each number with 44
and stopping at the first number greater than 44. The number is 77. Interchange 44 and 77
to obtain the list.

22 33 11 40 44 90 70 60 99 55 88 66

(Observe that the numbers to the left of 44 are each less than 44). Beginning with 77,
now scan this list from right to left, until meeting the first number less than 44. We don’t
meet such a number before meeting 44. This means all numbers have been scanned and
compared with 44. Furthermore all numbers less than 44 now from the sub list of
numbers to the left of 44, and all numbers greater than 44 now from the sub list of
numbers to the right of 44, as shown below.
22 33 11 40 44 90 70 60 99 55 88 66

22 33 11 40 44 90 70 60 99 55 88 66

22 33 11 40 90 70 60 99 55 88 66

11 33 22 40 66 70 60 99 55 88 90

11 22 33 40 66 70 60 90 55 88 99

22 66 70 60 88 55 90 99

11 66 70 60 88 55 90 99

33 40 66 70 60 88 55 99

33 55 70 60 88 66

40 55 66 60 88 70

55 60 66 88 70

55 60 66 88 70

55 60 88 70

55 70 88

60 88

70

The sorted array is

11 22 33 40 44 55 60 66 70 88 90 99
INSERTION SORT

Suppose an array A with n elements A [1], A [2],……..A[N] is in memory. The


insertion sort algorithm scans A from A [1] to A [N], inserting each element A [K] into
its proper position in the previously sorted sub array A [1], A[2],…[K -1]. That is

Pass 1: A[1] by itself is trivially sorted.


Pass 2: A[2] is inserted either before or after A[1] so that A[1], A[2] is sorted.
Pass 3: A[3] is inserted into its proper place in A[1], A[2] that is before A[1]
between A[1] and A[2] , or after A[2] , so that A[1], A[2] and A[3] is
sorted.
Pass 4: A[4] is inserted into its proper place in A[1], A[2] and A[3] so that:
A[1], A[2],A[3]and A[4] is sorted.
………………………………………………………………………………
Pass N. A[N] is inserted into its proper place in A[1], A[2]…….A[N-1] so
that: A[1], A[2]………A[N] is sorted.
ALGORITHM

(Insertion Sort) INSERTION (A, N)


This algorithm sorts the array A with N elements.
1. Repeat step 2 to 4 for K = 2,3,…..N.
2. Set TEMP = A [K] and P = K – 1.
3. Repeat while TEMP < A [P]
i. Set A [P + 1] = A [P] [Moves element forward]
ii. Set P = P – 1.
[End of loop]
4. Set A [P + 1] = TEMP. [Inserts element in proper place]
[End of step 1 loop]
5. Return

You might also like