-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.c
More file actions
78 lines (61 loc) · 1.14 KB
/
QuickSort.c
File metadata and controls
78 lines (61 loc) · 1.14 KB
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
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include "Sort.h"
//void sort(int* array, size_t length);
void QuickSort(int* array, size_t a, size_t b);
int partition(int* array, size_t a, size_t b);
void sort(int* array, size_t length)
{
if(length<=0)
return;
if(!array)
{
printf("Pointer doesn't point to an address \n") ;
exit(EXIT_FAILURE);
}
QuickSort(array,0,(length-1));
return;
}
void QuickSort(int* array, size_t a, size_t b)
{
if(!array)
{
printf("Pointer doesn't point to an address \n") ;
exit(EXIT_FAILURE);
}
int q;
if(a<b)
{
q=partition(array,a,b);
// -------- before calling the function we check if index is not negative.--------.
if(q>0)
QuickSort(array,a,q-1);
QuickSort(array,q+1,b);
}
return;
}
int partition(int* array, size_t a, size_t b)
{
if(!array)
{
printf("Pointer doesn't point to an address \n") ;
exit(EXIT_FAILURE);
}
size_t i=a-1;
size_t temp;
for(size_t j=a;j<b;j++)
{
if(array[j]<=array[b])
{
i+=1;
temp = array[j];
array[j]=array[i] ;
array[i]=temp;
}
}
temp=array[b];
array[b]=array[i+1];
array[i+1]=temp;
return i+1;z
}