-
Notifications
You must be signed in to change notification settings - Fork 0
/
selsortquicksort.c
125 lines (113 loc) · 2.22 KB
/
selsortquicksort.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//Selection Sort & Quick sort
//implementation using array
//by Simply
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
int arr[100];
int smallest(int k, int n, int pos)
{
int small;
small = arr[k];
pos = k;
for(int j=k+1; j<n; j++)
{
if(small > arr[j])
{
small = arr[j];
pos = j;
}
}
return pos;
}
void selection(int n)
{
int pos=0,temp;
for(int k=0; k<=n-1; k++)
{
pos = smallest(k,n,pos);
temp = arr[k];
arr[k] = arr[pos];
arr[pos] = temp;
}
}
int partition(int p, int r)
{
int pivot = arr[p];
int left = p+1;
int right = r;
while(left<=right)
{
while(left<=r && arr[left]<=pivot)
{
left++;
}
while(right>=p && arr[right]>pivot)
{
right--;
}
if(left<right)
{
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
}
}
int temp = arr[p];
arr[p] = arr[right];
arr[right] = temp;
return right;
}
void quick(int pivot, int right)
{
if(pivot < right)
{
int q = partition(pivot,right);
quick(pivot,q-1);
quick(q+1,right);
}
}
void display(int size)
{
printf("The current array is:");
for(int i=0; i<size; i++)
{
printf("%d ",arr[i]);
}
printf("\n");
}
void main()
{
int size,ch;
while(1)
{
printf("1.Define New Array\n2.Selection Sort\n3.Quick Sort\n4.Exit: ");
scanf("%d",&ch);
if(ch==1)
{
printf("Enter array limit: ");
scanf("%d",&size);
for(int i=0; i<size; i++)
{
printf("Enter: ");
scanf("%d",&arr[i]);
}
display(size);
}
else if(ch==2)
{
selection(size);
display(size);
}
else if(ch==3)
{
quick(0,size-1);
display(size);
}
else
{
break;
}
}
}