Cod sursa(job #1246935)

Utilizator Li4ickLi4ick Li4ick Data 21 octombrie 2014 20:22:56
Problema Sortare prin comparare Scor 0
Compilator c Status done
Runda Arhiva educationala Marime 1.13 kb
#include <stdio.h>

void swap(int *first, int *second) {
    int temp = *first;
        *first = *second;
        *second = temp;
}

void Insertion_Sort(int *array, int n) {

    int i, j;
    int temp = 0;
    for (i = 0; i < n - 1; i++) {
        j = i;
        temp = array[i + 1];
        while(array[j] > temp && j >= 0) {
            array[j + 1] = array[j];
            j--;
        }
        array[j+1] = temp;
    }
}

void Bucket_Sort(int *array, int n) {

    int i = 0;
    int k = 0, j;

    for (i = 0; i < n; i++) {
        for (j = k; j < n; j++) {
            if (array[j] / 10 < i) {
                swap(&array[k], &array[j]);
                k++;
            }
        }
    }

}


int main() {

    FILE *input = fopen("Bucket_Sort.in", "r");
    FILE *output = fopen("Bucket_Sort.out", "w");

    int n;
    fscanf(input, "%d", &n);

    int a[n];
    int i = 0;
    while (i < n) {
        fscanf(input, "%d", &a[i++]);
    }

    Bucket_Sort(a, n);
    Insertion_Sort(a, n);
    for (i = 0; i < n; i++) {
        fprintf(output, "%d ", a[i]);
    }

    return 0;
}