2 merge sort
TRANSCRIPT
-
7/25/2019 2 Merge Sort
1/3
Quick Sort Page 1of 3
PROGRAM 1: Sort a given set of elements using the Quick sort method
and determine the time required to sort the elements Re!eat the
e"!eriment for di#erent values of n$ the num%er of elements in the list
to %e sorted and !lot a gra!h of the time taken versus n &he elements
can %e read from a 'le or can %e generated using the random num%er
generator
A(GOR)&*M Quicksort(low, high, a)//Sorts a subarray by quicksort//Input: low : Position of the rst eleent of array a
high : Position of the last eleent of array aa : !rray consisting of unsorte" eleents
//#utput: a: !rray consisting of sorte" eleents
If low$highi" Partition(!, low, high) // i" is a split position
Quicksort(!,low, i"%&)Quicksort(!, i"'&,high)
A(GOR)&*M Partition+lo,$ high$ a-//Partitions a subarray by using its rst eleent as a piot//Input: low : Position of the rst eleent of array a
high : Position of the last eleent of array a
a : !rray consisting of unsorte" eleents//#utput: ! partition of !low***high+, with the split position returne" as this
functions alue*
-ey !low+
i low , j high+1
while (i$.)
"o i i' & while (key0.ai+)
"o j1 while (key$a+)
if (i$) swap(!i+, !+)en" while swap(!low+, !+)
return .
-
7/25/2019 2 Merge Sort
2/3
Quick Sort Page /of 3
Program
#include
#include // for rand function#include //for CLOCKS_PER_SEC
#include //for struct
cloc!_t start"end
struct tms st"ed
int $artition%int lo&"int hi'h"int a()*+
int i","!e"tem$
i-lo&,-hi'h
!e-a(lo&)
&hile%i-a(i)*do ,00
&hile%!e
-
7/25/2019 2 Merge Sort
3/3
Quick Sort Page 3of 3
3uic!_sort%mid"hi'h"a*
1
1int main%*
+
int n"i"a(45)
$rintf%67nEnter the number of elements 86*scanf%69d6":n*
for %i-5i