Krolyxon
Commit 0980f89

Branches

Refs →

trossrcsort.c

41 lines · 762 B · C

1#include "utils.h"
2#include <stdio.h>
3void bubblesort(int a[], int n) {
4 int comparisons = 0;
5 for (int i = 0; i < n; i++) {
6 for (int j = 1; j < n - i; j++) {
7 comparisons++;
8 if (a[j - 1] > a[j]) {
9 swap(a, j, j - 1);
10 }
11 }
12 printarr(a, n);
13 printf("\n");
14 }
15}
16
17void selectionsort(int a[], int n) {
18 for (int i = 0; i < n; i++) {
19 int small = i;
20 for (int j = i; j < n; j++) {
21 if (a[j] < a[small]) {
22 small = j;
23 }
24 }
25 swap(a, i, small);
26 printarr(a, n);
27 printf("\n");
28 }
29}
30
31void insertionsort(int a[], int n) {
32 for (int i = 1; i < n; i++) {
33 int j = i;
34 while (j > 0 && a[j - 1] > a[j]) {
35 swap(a, j, j - 1);
36 j--;
37 }
38 printarr(a, n);
39 printf("\n");
40 }
41}