nya
This commit is contained in:
parent
6161b74ffc
commit
eeffd774de
11 changed files with 265 additions and 703 deletions
30
sort/main.c
Normal file
30
sort/main.c
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include "sorts.c"
|
||||
#define ALEN 1000
|
||||
#define DISPLEN 25
|
||||
#define THRESHOLD 25
|
||||
|
||||
int main() {
|
||||
srand(time(NULL));
|
||||
int a[ALEN];
|
||||
for (int i = 0; i < ALEN; a[i++] = rand() % (2 * ALEN));
|
||||
|
||||
printf("Before:\n");
|
||||
for (int i = 0; i < DISPLEN; ++i) {
|
||||
printf("%d ", a[i]);
|
||||
}
|
||||
putchar('\n');
|
||||
|
||||
//bubblesort(a, ALEN);
|
||||
insertion(a, ALEN);
|
||||
printf("After:\n");
|
||||
for (int i = 0; i < DISPLEN; ++i) {
|
||||
printf("%d ", a[i]);
|
||||
}
|
||||
putchar('\n');
|
||||
|
||||
return 0;
|
||||
}
|
||||
63
sort/sorts.c
Normal file
63
sort/sorts.c
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
void bubblesort(int *a, int n);
|
||||
|
||||
void insertion(int *a, int n);
|
||||
|
||||
/* Сортировка пузырьком, сложность - O(n^2), устойчива */
|
||||
void bubblesort(int *a, int n) {
|
||||
bool sorted = false;
|
||||
while (!sorted) {
|
||||
sorted = true;
|
||||
for (int i = 0; i < n-1; i++) {
|
||||
if (a[i] > a[i+1]) {
|
||||
int t = a[i];
|
||||
a[i] = a[i+1];
|
||||
a[i+1] = t;
|
||||
sorted = false;
|
||||
}
|
||||
}
|
||||
n--;
|
||||
}
|
||||
}
|
||||
|
||||
/* Сортировка вставками, для упорядоченного массива - O(n), в худшем случае - O(n^2), устойчива */
|
||||
void insertion(int *a, int n) {
|
||||
for (int i = n-1; i > 0; i--) {
|
||||
if (a[i-1] > a[i]) {
|
||||
int t = a[i-1]; a[i-1] = a[i]; a[i] = t;
|
||||
}
|
||||
}
|
||||
for (int i = 2; i < n; i++) {
|
||||
int j = i;
|
||||
int tmp = a[i];
|
||||
while (tmp < a[j-1]) {
|
||||
a[j] = a[j-1]; j--;
|
||||
}
|
||||
a[j] = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
/* Функция слияния двух массивов, сложность - O(n) */
|
||||
void merge(int *a, int low, int mid, int high, int *aux) {
|
||||
int i,j;
|
||||
for (i = mid+1; i > low; i--) aux[i-1] = a[i-1];
|
||||
for (j = mid; j < high; j++) aux[high+mid-j]=a[j+1];
|
||||
for (int k = low; k <= high; k++) {
|
||||
if (aux[j] < aux[i])
|
||||
a[k] = aux[j--];
|
||||
else
|
||||
a[k] = aux[i++];
|
||||
}
|
||||
}
|
||||
|
||||
void mergeSort(int *a, int low, int high) {
|
||||
if (high - low < THRESHOLD) {
|
||||
insertion(a, high-low);
|
||||
}
|
||||
else {
|
||||
int mid = (low + high)/2;
|
||||
mergeSort(a, low, mid);
|
||||
mergeSort(a, mid+1, high);
|
||||
merge(a, low, mid, high);
|
||||
|
||||
}
|
||||
}
|
||||
Reference in a new issue