Quick Sort (Divide and Conquer Technique) - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Community

demo-image

Quick Sort (Divide and Conquer Technique)

Share This
Source code for Quick Sort
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
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
int partition(int *arr, int p, int q)
{
int i = p - 1, j;
int x = arr[q];
for(j = p; j < q; j++){
if(arr[j] <= x){
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[q]);
return i + 1;
}
void quickSort(int *arr, int p, int q){
if(p < q){
int m = partition(arr, p, q);
quickSort(arr, p, m - 1);
quickSort(arr, m + 1, q);
}
}
int main()
{
int *arr, n, i;
printf("Enter size of array:");
scanf("%d", &n);
printf("Enter items of the array:");
for(i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Happy Exploring!

Comment Using!!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.