-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSorting.c
More file actions
63 lines (53 loc) · 1.32 KB
/
Sorting.c
File metadata and controls
63 lines (53 loc) · 1.32 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
#include <stdio.h>
void PrintArray( int *a , int n){
printf("The array is as following : \n");
for(int i = 0 ; i < n ; i++){
printf("%d\t",a[i]);
}
printf("\n");
}
void swap(int *x, int *y) {
if (x != y) {
*x = *x ^ *y;
*y = *x ^ *y;
*x = *x ^ *y;
}
}
int bubblesort( int *a , int n){
printf("Running Bubble Sort.............\n");
int i , j ;
for( i = 0 ; i < n-1 ; i++){
for( j = 0 ; j < n-1-i ; j++){
if(a[j]>a[j+1]){
swap( &a[j] , &a[j+1] );
}
}
}
}
void selectionsort(int *a, int n){
printf("Running Selection Sort.............\n");
int i , j , min;
for ( i = 0; i < n -1 ; i++){
min = i;
for( j = i+1 ; j < n ; j++ ){
if(a[min]>a[j]){
min = j;
}
}
swap( &a[i] , &a[min]);
}
}
int main(){
int n;
printf("Enter the number of elements to insert : ");
scanf("%d",&n);
int a[n];
for (int i = 0 ; i < n ; i++){
printf("Enter the %d no. element : ", i+1);
scanf("%d",&a[i]);
}
PrintArray( a , n); // Array before Sorting
selectionsort( a , n); // Sorting fx.
PrintArray( a , n); // Array after Sorting.
return 0;
}