-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassign4-2.c
More file actions
50 lines (50 loc) · 742 Bytes
/
assign4-2.c
File metadata and controls
50 lines (50 loc) · 742 Bytes
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
// SORTING AN ARRAY
/* There are many Techniques to sort an array,
Here we will be using
Bubble Sort and Selection Sort*/
//Bubble Sort
void swap(int &x,int &y)
{
int temp;
temp=x;
x=y;
y=temp;
}
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
/*for(int i=0;i<n-1;i++) // Bubble Sort
{
for(int j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
swap(a[j],a[j+1]);
}
}*/
// Selection Sort
for(int i=0;i<=n-2;i++)
{
int min=i;
for(int j=i+1;j<n;j++)
{
if(a[j]<a[min])
{
min=j;
}
}
swap(a[min],a[i]);
}
printf("Sorted Array=\n");
for(int i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
return 0;
}