-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment 7
More file actions
82 lines (71 loc) · 1.25 KB
/
Assignment 7
File metadata and controls
82 lines (71 loc) · 1.25 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
77
78
79
80
81
82
ASSIGNMENT NO.7
TITLE: Implementation of quick sort.
#include<stdio.h>
void swap (int a[], int left, int right)
{
int temp;
temp=a[left];
a[left]=a[right];
a[right]=temp;
}
voidprintarr(int a[], int n);
void quicksort( int a[], int low, inthigh,int n )
{
int pivot;
if ( high > low )
{
pivot = part( a, low, high );
printf("\n%d is pivot element\n",a[pivot]);
printarr(a,n);
quicksort( a, low, pivot-1,n );
quicksort( a, pivot+1, high,n );
}
}
int part( int a[], int low, int high )
{
int left, right;
intpivot_item;
int pivot = left = low;
pivot_item = a[low];
right = high;
while ( left < right )
{
while( a[left] <= pivot_item )
{
left++;
}
while( a[right] >pivot_item )
{
right--;
}
if ( left < right )
{
swap(a,left,right);
}
}
a[low] = a[right];
a[right] = pivot_item;
return right;
}
int main()
{
int a[50], i, n;
printf("\nEnter no. of elements: ");
scanf("%d", &n);
printf("\nEnter the elements: \n");
for (i=0; i<n; i++)
scanf ("%d", &a[i]);
printf("\nUnsorted elements: \n");
printarr(a,n);
quicksort(a,0,n-1,n);
printf("\nSorted elements: \n");
printarr(a,n);
}
void printarr(int a[], int n)
{
int i;
printf("\n");
for (i=0; i<n; i++)
printf(" %d ", a[i]);
printf("\n");
}