-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsort_Bubble_Insertion_Selection.cpp
More file actions
128 lines (106 loc) · 2.4 KB
/
sort_Bubble_Insertion_Selection.cpp
File metadata and controls
128 lines (106 loc) · 2.4 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
118
119
120
121
122
123
124
125
126
127
#include<bits/stdc++.h>
using namespace std;
void sortBubble()
{
int n;
printf("Enter array size :");
scanf("%d",&n);
int a[n];
printf("Enter %d Value :",n);
for(int i=0; i<n; i++) scanf("%d",&a[i]);
for(int i=0; i<n-1; i++)
{
for(int j=0; j<n-i-1; j++)
{
if(a[j]>a[j+1])
{
int tmp=a[j];
a[j]=a[j+1];
a[j+1]=tmp;
}
}
}
for(int i=0; i<n; i++) printf("%d ",a[i]);
printf("\n");
}
void SortInsertion()
{
int n;
printf("Enter array size :");
scanf("%d",&n);
int a[n];
printf("Enter %d Value :",n);
for(int i=0; i<n; i++) scanf("%d",&a[i]);
for(int i=1; i<n; i++)
{
//3:: 1 2 6 3
int hole=i;
int val=a[hole];
while(hole>0 && a[hole-1]>val)
{
a[hole]=a[hole-1];
//cout<<a[hole]<<" "<<a[hole-1]<<endl ;
hole--;
}
a[hole]=val;
}
printf("Sorting Array :");
for(int i=0; i<n; i++) printf("%d ",a[i]);
printf("\n");
}
void SortSelection()
{
int n;
printf("Enter array size :");
scanf("%d",&n);
int a[n];
printf("Enter %d Value :",n);
for(int i=0; i<n; i++) scanf("%d",&a[i]);
for(int i=0; i<n; i++)
{
//2 4 6 3
int idx=i;
for(int j=i+1; j<n; j++)
{
if(a[idx]>a[j])
{
idx=j;
}
}
int t=a[i];
a[i]=a[idx];
a[idx]=t;
}
printf("Sorting Array :");
for(int i=0; i<n; i++) printf("%d ",a[i]);
printf("\n");
}
int main()
{
int choice;
printf(" 1.Bubble Sort\n");
printf(" 2.Insertion Sort\n");
printf(" 3.Selection Sort\n");
while(1)
{
printf("\nEnter choice :");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("_________Bubble Sort_________\n");
sortBubble();
break;
case 2:
printf("_________Insertion Sort_________\n");
SortInsertion();
break;
case 3:
printf("_________Selection Sort_________\n");
SortSelection();
break;
case 4:
continue;
}
}
}