-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicSort.c
More file actions
74 lines (60 loc) · 1.03 KB
/
BasicSort.c
File metadata and controls
74 lines (60 loc) · 1.03 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
#include <stdio.h>
void sort(int a[],int n);
void bsort(int a[], int n);
void isort(int a[], int n);
void SWAP(int *x,int *y);
int main(){
int i;
int a[5]={3,1,2,4,5};
int b[5]={2,4,5,3,1};
int c[5]={5,4,1,3,2};
sort(a,5);
bsort(b,5);
isort(c,5);
for(i=0; i<5; i++)
printf("%d ",a[i]);
printf("\n");
for(i=0; i<5; i++)
printf("%d ",b[i]);
printf("\n");
for(i=0; i<5; i++)
printf("%d ",c[i]);
return 0;
}
//선택정렬
void sort(int a[],int n){
int last,i,max;
for(last=n-1;last>0;last--){
max=last;
for(i=0;i<last;i++)
if(a[max]<a[i])
max=i;
SWAP(&a[last],&a[max]);
}
}
//버블정렬
void bsort(int a[], int n){
int last,i;
for(last=n-1; last>0; last--)
for(i=0; i<last; i++)
if(a[i]>a[i+1])
SWAP(&a[i],&a[i+1]);
}
//삽입정렬
void isort(int a[],int n){
int i,loc,newItem;
for(i=1; i<n; i++){
loc=i-1;
newItem=a[i];
while(loc>=0 && newItem<a[loc]){
a[loc+1] = a[loc];
loc=loc-1;
}
a[loc+1]=newItem;
}
}
void SWAP(int *x, int *y){
int temp=*x;
*x=*y;
*y=temp;
}