C Program For Bubble Sort | C Programming

C Program For Bubble Sort by Generating Random Numbers and Using time.h Function

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void bubble_sort(int[], int);
int main()
{
    int list[100], n, i;
    time_t t;
    printf("Enter the max number \n");
    scanf("%d", &n);
    srand((unsigned)time(&t));
    for (i = 0; i < n; i++)
    {
        list[i] = rand() % 100;
    }
    for (i = 0; i < n; i++)
    {
        printf("%d\t", list[i]);
    }
    bubble_sort(list, n);
    printf("\n\nTime taken to complete the bubblesort %u\n", clock() / CLOCKS_PER_SEC);
    printf("\nThe sorted list is:");
    for (i = 0; i < n; i++)
        printf(" %d\t", list[i]);
    return 0;
}
void bubble_sort(int list[], int n)
{
    int temp, i, j;
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < n - i - 1; j++)
        {
            if (list[j] > list[j + 1])
                if (list[j] > list[j + 1])
                {
                    temp = list[j];
                    list[j] = list[j + 1];
                    list[j + 1] = temp;
                }
        }
    }
}

Output

C Program For Bubble Sort by Generating Random Numbers and Using time.h Function

Comments

Popular posts from this blog

C Program for SCAN Disk Scheduling Algorithm | C Programming

C program to Find Cartesian Product of Two Sets | C programming

C Program To Check The String Is Valid Identifier Or Not | C Programming