-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsorting tech.cpp
116 lines (91 loc) · 1.73 KB
/
sorting tech.cpp
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
#include<stdio.h>
#include<stdlib.h>
#include<stdio.h>
void swap(int *a,int *b);
void print(int arr[],int n);
void selection_sort(int arr[],int n);
void bubble_sort(int arr[],int n);
void insertion_sort(int arr[],int n);
int main()
{
int arr[100],i,j,n,opt;
printf("\n Enter the number of elements:");
scanf("%d",&n);
printf("\n Enter the numbers :");
for(i=0;i<n;i++)
scanf("\t%d",&arr[i]);
while(1)
{
printf("\n ***sorting techniques***\n1.bubble sort\n2.selection sort\n3.insertion sort\n 4.exit");
printf("\n Enter your choice...:");
scanf("%d",&opt);
switch(opt)
{
case 1:bubble_sort(arr,n);break;
case 2:selection_sort(arr,n);break;
case 3:insertion_sort(arr,n);break;
case 4:exit(0);
default:printf("\n Enter correct option..");
}
}
}
void print(int arr[],int n)
{
int i;
for(i=0;i<n;i++)
printf("\t %d",arr[i]);
}
void swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
void selection_sort(int arr[],int n)
{
int i,j,smaller,temp;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[i]>arr[j])
swap(&arr[i],&arr[j]);
}
}
printf("\n output of selection sort...");
print(arr, n);
}
void bubble_sort(int arr[],int n)
{
int i,j,temp;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(arr[j]>arr[j+1])
{
swap(&arr[j],&arr[j+1]);
}
}
}
printf("\n output of bubble sort...");
print(arr, n);
}
void insertion_sort(int arr[],int n)
{
int i,j,cur;
for(i=1;i<n;i++)
{
cur=arr[i];
j=i-1;
while(j>=0 && arr[j]>cur)
{
arr[j+1]=arr[j];
j=j-1;
}
arr[j+1]=cur;
}
printf("\n output of insertion sort...");
print(arr,n);
}