-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort.c
More file actions
194 lines (156 loc) · 4.29 KB
/
sort.c
File metadata and controls
194 lines (156 loc) · 4.29 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//Aluno: João Victor Breches Alves - Nusp: 13677142 - Universidade de São Paulo
#include "sort.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
//BubbleSort function
void bubbleSort(TAD *t){
elem sup;
//Compare the elements and, if necessary, swap them
for(int i=0; i < t->size -1; i++){
for(int j=0; j < t->size -1 - i; j++){
if(t->element[j] > t->element[j+1]){
sup = t->element[j];
t->element[j] = t->element[j+1];
t->element[j+1] = sup;
}
}
}
}
//Better BubbleSort function
void better_bubble(TAD *t){
long perm = 1, step = 1;
elem sup;
for(long i=0; i< t->size -1; i++){
perm = 0;
//Compare the elements and, if necessary, swap them
for(long j = 0; j<t->size-1 -i; j++)
if(t->element[j] > t->element[j+1]){
sup = t->element[j];
t->element[j] = t->element[j+1];
t->element[j+1] = sup;
perm = 1;
}
perm++;
}
}
//QuickSort function
void quickSort_r(TAD *t, long low, long high) {
long i, j;
elem sup;
//Partition the list
i = low;
for(j = low; j < high; j++){
//verify pivot
if(t->element[j] < t->element[high]){
//swap
sup = t->element[j];
t->element[j] = t->element[j+1];
t->element[j+1] = sup;
}
}
sup = t->element[high];
t->element[high] = t->element[i];
t->element[i] = sup;
if(low < i-1)
quickSort_r(t, low, i-1);
if(i+1<high)
quickSort_r(t, i+1, high);
}
void quickSort(TAD *t){
return quickSort_r(t, 0, t->size-1);
}
void inverseSort(TAD *t){
bool perm = true;
for (int i = 0; i < t->size - 1 && perm; i++) {
perm = false;
elem sup = 0;
for (int j = 0; j < t->size - i - 1; j++) {
if (t->element[j] < t->element[j + 1]) {
sup = t->element[j];
t->element[j] = t->element[j+1];
t->element[j+1] = sup;
perm = true;
}
}
}
}
void Couting_sort(TAD *t, int n, int posicao){
int *B=(int*)malloc(10*sizeof(int));
for(int i=0;i<10;i++){
B[i]=0;
}
int chave;
for(int i=0;i<n;i++){
chave=t->element[i]/posicao;
chave=chave%10;
B[chave]+=1;
}
for(int i=1;i<10;i++){
B[i]+=B[i-1];
}
int *C=(int*)malloc(n*sizeof(int));
for(int i=n-1;i>=0;i--){
chave=t->element[i]/posicao;
chave=chave%10;
B[chave]-=1;
C[B[chave]]=t->element[i];
}
for(int i=0;i<n;i++){
t->element[i]=C[i];
}
}
void Radix_sort(TAD *t, int n){
int maior=t->element[0];
for(int i=0;i<n;i++){
if(t->element[i]>maior){
maior=t->element[i];
}
}
int posicao=1;
while(maior>posicao){
Couting_sort(t, n, posicao);
posicao*=10;
}
}
void max_heapify(TAD *t, int father, int n){
int son = father*2;
int aux;
if (son > n) return;
if (t->element[son] > t->element[father] || (son + 1 <= n && t->element[son + 1] > t->element[father]) ){
if (son + 1 <= n && t->element[son + 1] > t->element[son]){
aux = t->element[father];
t->element[father] = t->element[son + 1];
t->element[son + 1] = aux;
} else {
aux = t->element[father];
t->element[father] = t->element[son];
t->element[son] = aux;
}
}
}
void min_heapify(TAD *t, int father, int n){
int son = father*2;
int aux;
if (son > n) return;
if (t->element[son] < t->element[father] || (son + 1 <= n && t->element[son + 1] < t->element[father] )){
if (son + 1 <= n && t->element[son + 1] < t->element[son]){
aux = t->element[father];
t->element[father] = t->element[son + 1];
t->element[son + 1] = aux;
} else {
aux = t->element[father];
t->element[father] = t->element[son];
t->element[son] = aux;
}
}
}
void heap_sort(TAD *t){
elem aux;
for (int i=(int)(t->size/2) - 1; i >= 1; i--){
max_heapify(t, i, t->size);
}
aux = t->element[0];
t->element[0] = t->element[t->size-1];
t->element[t->size-1] = aux;
}