bubble sort

#include <stdio.h>
#include <stdlib.h>
#define ARRAY_SIZE 100
void local_swap(int *a, int *b);
void bubble_sort(int a[], const int size);
int main(void)
{
    int i;
    int a[ARRAY_SIZE];
    
    srand(time(NULL)); /* set the seed with current time to make rand() produce different data every time*/
    
    for (i = 0; i < ARRAY_SIZE; ++i) /* initialize the array with random data */
    {
            a[i] = (rand() % RAND_MAX);
    }
    
    printf("Before sort :\n");
    for (i = 0; i < ARRAY_SIZE; ++i)
    {
            printf("%d%c", a[i], (((i % 10) == 9) ? ('\n') : ('\t')));
    }
    
    bubble_sort(a, ARRAY_SIZE);
    
    printf("\nAfter sorted :\n");
    for (i = 0; i < ARRAY_SIZE; ++i)
    {
            printf("%d%c", a[i], (((i % 10) == 9) ? ('\n') : ('\t')));
    }
    
    system("pause");
    return 0;
}
void bubble_sort(int a[], const int size)
{
     int temp = 0;
     int start_index = 0;
     int last_item_index = size - 1;
     for(start_index = 0; start_index < size; ++start_index)
     {
            for(last_item_index = size - 1; last_item_index > start_index; --last_item_index)
            {
                    if(a[last_item_index] < a[last_item_index - 1])
                            local_swap((a + last_item_index), (a + last_item_index - 1));
            }
     }
}
void local_swap(int *a, int *b)
{
     int temp = 0;
     if((*a) < (*b))
     {
           temp = *a;
           *a = *b;
           *b = temp;
     }
}

你可能感兴趣的:(职场,休闲)