-
Notifications
You must be signed in to change notification settings - Fork 1
/
exp 13: sorting algos _1.c
77 lines (70 loc) · 1.58 KB
/
exp 13: sorting algos _1.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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
void bubblesort(int arr[], int n){
for(int i=0;i<n;i++){
for(int j=0;j<n-1-i;j++){
if (arr[j]>arr[j+1]){
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
void insertionsort(int arr[], int n){
for(int i=0;i<n;i++){
int key=arr[i];
int j=i-1;
while(j>=0 && arr[j]>key){
arr[j+1]=arr[j];
j--;
}
arr[j+1]=key;
}
}
void selectionsort(int arr[], int n){
for(int i=0;i<n-1;i++){
int minind=i;
for(int j=i+1;j<n;j++){
if(arr[j]<arr[minind]){
minind=j;
}
}
int temp=arr[i];
arr[i]=arr[minind];
arr[minind]=temp;
}
}
int main() {
int a,n,choice;
printf("enter the no.of elements in the array : ");
scanf("%d",&n);
int arr[n];
for(int i=0;i<n;i++){
printf("element %d: ",i+1);
scanf("%d",&arr[i]);
}
printf("\nchose a sorting method for your array:\n1.bubble sort\n2.insertion sort\n3.selection sort\n4.exit\n ");
scanf("%d",&choice);
switch(choice){
case 1:
bubblesort(arr,n);
break;
case 2:
insertionsort(arr,n);
break;
case 3:
selectionsort(arr,n);
break;
case 4:
exit(0);
break;
}
printf("\nthe SORTED array is: \n");
for(int i=0;i<n;i++){
printf("%d ",arr[i]);
}
return 0;
}