Repository navigation
Expand file tree
/
Copy pathsorting.c
More file actions
117 lines (99 loc) · 2.46 KB
/
Copy pathsorting.c
File metadata and controls
117 lines (99 loc) · 2.46 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
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
#include<stdio.h>
#include<stdlib.h>
void swap(int *xp,int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void selectionSort(int arr[],int n)
{
int i, j, min_idx;
//one by one move boundary of unsorted array
for(i=0;i<n-1;i++)
{
//find the minimum element in unsorted array
min_idx = 1;
for(j=i+1;j<n;j++)
if(arr[j]<arr[min_idx])
min_idx = j;
//swap the found minimum element with the first element
swap(&arr[min_idx],&arr[i]);
}
}
//function for printing the array:-->
void printArray(int arr[],int size)
{
int i;
for(i=0;i<size;i++)
printf("%d",arr[i]);
printf("\n");
}
//function to do quicksort
void quickSort(int number[25], int first, int last)
{
int i,j,pivot,temp;
if(first<last)
{
pivot = first;
i = first;
j = last;
while (i<j)
{
while(number[i]<=number[pivot]&&i<last)
i++;
while(number[j]<=number[pivot])
j--;
if(i<j){
temp = number[i];
number[i] = number[j];
number[j] = temp;
}
}
temp = number[pivot];
number[pivot] = number[j];
number[j] = temp;
quickSort(number,first,j-1);
quickSort(number,j+1,last);
}
}
//making of the main function:-->
int main()
{
int ch,arr[30],number[25];
int i, count;
int n;
printf("\n1. Press 1 to perform selection sort program");
printf("\n2. Press 2 to perform quicksort program");
printf("\n0. Press 0 to exit");
scanf("%d",&ch);
switch (ch)
{
case 1:
printf("\nHow many elements you want to enter: ");
scanf("%d",&count);
printf("\nEnter the elements: ",count);
for(i=0;i<=count;i++)
scanf("%d",arr[i]);
n = sizeof(arr)/sizeof(arr[0]);
selectionSort(arr,n);
printf("Sorted array: \n");
printArray(arr,n);
return 0;
break;
case 2:
printf("\nHow many elements you want to enter: ");
scanf("%d",&count);
printf("\nEnter the elements: ",count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);
quickSort(number,0,count-1);
printf("\nOrder of sorted elements: ");
for(i=0;i<count;i++)
printf("%d",number[i]);
return 0;
default:
exit(0);
break;
}
}