-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickSort.c
69 lines (48 loc) · 1.14 KB
/
QuickSort.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
#include<stdio.h>
#include<stdlib.h>
void quicksort(int,int);
int partition(int,int);
void swap(int,int);
int *arr;
void main(){
int arraySize;
int i;
printf("\nEnter the size of array ");
scanf("%d",&arraySize);
arr = malloc(sizeof(int)*arraySize);
for(i = 0;i<arraySize;i++)
scanf("%d",&arr[i]);
quicksort(0,arraySize-1);
printf("\nHere is your soted list......\n");
for(i = 0;i<arraySize;i++)
printf("\t%d",arr[i]);
printf("\n");
}
void quicksort(int start,int end){
int pIndex;
if(start<end){
pIndex = partition(start,end);
quicksort(start,pIndex-1);
quicksort(pIndex+1,end);
}
}
int partition(int start,int end){
int pivot = arr[end];
int i;
int pIndex = start;
int temp;
for(i=start;i<end;i++){
if(arr[i]<=pivot){
swap(i,pIndex);
pIndex++;
}
}
swap(i,pIndex);
return pIndex;
}
void swap(int index1,int index2){
int temp;
temp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = temp;
}