-
Notifications
You must be signed in to change notification settings - Fork 1
/
sort.c
135 lines (125 loc) · 2.13 KB
/
sort.c
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
128
129
130
131
132
133
134
135
#include <stdio.h>
#include <stdlib.h>
void troca(int *a, int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
void selection_sort(int *v, int n)
{
int i, j, min;
for ( i = 0; i < n; i++)
{
min=i;
for ( j = i+1; j<n; j++)
{
if(v[j]<v[min])
{
min=j;
}
troca(&v[i], &v[min]);
}
}
}
void bubble_sort(int *v, int n)
{
int i, j;
for(i=n-1; i>=0; i--)
{
for ( j = 0; j < i; j++)
{
if(v[j]>v[j+1])
{
troca(&v[j], &v[j+1]);
}
}
}
}
void quicksort(int *values, int began, int end)
{
int i, j, pivo, aux;
i = began;
j = end-1;
pivo = values[(began + end) / 2];
while(i <= j)
{
while(values[i] < pivo && i < end)
{
i++;
}
while(values[j] > pivo && j > began)
{
j--;
}
if(i <= j)
{
aux = values[i];
values[i] = values[j];
values[j] = aux;
i++;
j--;
}
}
if(j > began)
quicksort(values, began, j+1);
if(i < end)
quicksort(values, i, end);
}
void insertion_sort(int *v, int n)
{
int i, j, chave;
for ( i = 1; i < n; i++)
{
chave = v[i];
j=i-1;
while (j>=0 && v[j]>chave)
{
v[j+1]=v[j];
j--;
}
v[j+1]=chave;
}
}
int main()
{
FILE *myFile;
myFile = fopen("list.txt", "r");
int *array;
int i, size, x;
printf("insert array size.\n");
scanf("%d", &size);
array = malloc(size*sizeof(int));
for (i = 0; i < size; i++)
{
fscanf(myFile, "%d", &array[i]);
}
fail:
printf("choose the sorting algorithym\n1 bubble\n2 selection\n3 quick\n4 insertion\n");
scanf("%d", &x);
if(x>4 || x<1)
{
goto fail;
}
else if(x==1)
{
bubble_sort(array, size);
}
else if(x==2)
{
selection_sort(array, size);
}
else if(x==3)
{
quicksort(array, size, 0);
}
else
{
insertion_sort(array, size);
}
for ( i = 0; i < size; i++)
{
printf("%d\n", array[i]);
}
}