-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquick_sort.c
89 lines (60 loc) · 1.36 KB
/
quick_sort.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include<stdio.h>
#include <time.h>
int main()
{
clock_t start, end;
double cpu_time_used;
start = clock();
int n=0;
int arr[100];
int len;
// after variable
int swap;
int pivot;
int counter1=0;
int counter2=0;
printf("enter length of array and data in array ");
scanf("%d",&len);
for(n;n<len;n++)
{
scanf("%d",&arr[n]);
}
n=0;
printf("array is \n ");
for(n;n<len;n++)
{
printf("arr %d == %d ",n,arr[n]);
}
printf(" \n ");
// applying qucik sort
// counter 1 is for each pass
// counter 2 is for increment
pivot = len - 1 ;
while(counter2 < pivot)
{
if(arr[pivot] < arr[counter2])
{
counter2++;
}
else if(arr[pivot] > arr[counter2])
{
counter1++;
swap=arr[counter2];
arr[counter2]=arr[counter1];
arr[counter1]=swap;
printf("swap %d & %d \n arr[%d] > arr[%d] or %d > %d ", arr[counter2] , arr[counter1],pivot,counter2,arr[pivot] , arr[counter2] );
// printf(" arr[%d] > arr[%d] or %d > %d ",pivot,counter2,arr[pivot] , arr[counter2]);
counter2++;
}
printf("after counter 1== %d counter 2 == %d \n ",counter1,counter2);
}
n=0;
printf("new array is \n ");
for(n;n<len;n++)
{
printf("%d ",arr[n]);
}
end = clock();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("time used is %f",cpu_time_used);
}