搜尋此網誌

[C/C++][Selection Sort]

/*
* File Name: SelectionSort.c
* Author: MH
* Since 2013/02/22
* Toolkit: Dev C++ 4.9.9.9
*/

#include <stdio.h>
#include <stdlib.h>

void swap (int *a, int *b){

    int tmp;

    tmp = *a;
    *a = *b;
    *b = tmp;
}

int main(int argc, char *argv[]){

    int i, j, n=5;
    int list[5] = {10, 7, 19, 5, 6};

    printf("Before Slelction Sort\n");

    for (i=0; i<n; i++)
        printf("list[%d] = %d\n", i, list[i]);

    for (i=0; i<n; i++)
        for (j=i+1; j<n; j++)
            if (list[i]>list[j])
                swap(&list[i], &list[j]);

    printf("\nAfter Slelction Sort\n");

    for (i=0; i<n; i++)
        printf("list[%d] = %d\n", i, list[i]);

    system("PAUSE");
    return 0;
}

沒有留言:

張貼留言