mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
bubble sort optimization (#573)
This commit is contained in:
parent
3dc947213a
commit
e43024e8f5
@ -1,20 +1,33 @@
|
|||||||
// sorting of array list using bubble sort
|
/**
|
||||||
|
* @file
|
||||||
|
* @brief [Bubble sort](https://en.wikipedia.org/wiki/Bubble_sort) algorithm
|
||||||
|
* implementation
|
||||||
|
*/
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
/*Displays the array, passed to this method*/
|
/**
|
||||||
void display(int *arr, int n)
|
* Display elements of array
|
||||||
|
* @param arr array to be display
|
||||||
|
* @param n length of array
|
||||||
|
*/
|
||||||
|
void display(const int *arr, int n)
|
||||||
{
|
{
|
||||||
int i;
|
for (int i = 0; i < n; i++)
|
||||||
for (i = 0; i < n; i++)
|
|
||||||
{
|
{
|
||||||
printf("%d ", arr[i]);
|
printf("%d ", arr[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
/*Swap function to swap two values*/
|
/**
|
||||||
|
* Swap two values by using pointer
|
||||||
|
* @param first first pointer of first number
|
||||||
|
* @param second second pointer of second number
|
||||||
|
*/
|
||||||
void swap(int *first, int *second)
|
void swap(int *first, int *second)
|
||||||
{
|
{
|
||||||
int temp = *first;
|
int temp = *first;
|
||||||
@ -22,46 +35,61 @@ void swap(int *first, int *second)
|
|||||||
*second = temp;
|
*second = temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*This is where the sorting of the array takes place
|
/**
|
||||||
arr[] --- Array to be sorted
|
* Bubble sort algorithm implementation
|
||||||
size --- Array Size
|
* @param arr array to be sorted
|
||||||
|
* @param size size of array
|
||||||
*/
|
*/
|
||||||
void bubbleSort(int *arr, int size)
|
void bubbleSort(int *arr, int size)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < size - 1; i++)
|
for (int i = 0; i < size - 1; i++)
|
||||||
{
|
{ /* for each array index */
|
||||||
|
bool swapped = false; /* flag to check if any changes had to be made */
|
||||||
|
/* perform iterations until no more changes were made or outer loop
|
||||||
|
executed for all array indices */
|
||||||
for (int j = 0; j < size - 1 - i; j++)
|
for (int j = 0; j < size - 1 - i; j++)
|
||||||
{
|
{ /* for each element in the array */
|
||||||
if (arr[j] > arr[j + 1])
|
if (arr[j] > arr[j + 1])
|
||||||
{
|
{ /* if the order of successive elements needs update */
|
||||||
swap(&arr[j], &arr[j + 1]);
|
swap(&arr[j], &arr[j + 1]);
|
||||||
|
swapped = true; /* set flag */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (!swapped)
|
||||||
|
{
|
||||||
|
/* since no more updates we made, the array is already sorted
|
||||||
|
this is an optimization for early termination */
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test function
|
||||||
|
*/
|
||||||
|
void test()
|
||||||
|
{
|
||||||
|
const int size = 10;
|
||||||
|
int *arr = (int *)calloc(size, sizeof(int));
|
||||||
|
|
||||||
|
/* generate size random numbers from 0 to 100 */
|
||||||
|
for (int i = 0; i < size; i++)
|
||||||
|
{
|
||||||
|
arr[i] = rand() % 100;
|
||||||
|
}
|
||||||
|
bubbleSort(arr, size);
|
||||||
|
for (int i = 0; i < size - 1; ++i)
|
||||||
|
{
|
||||||
|
assert(arr[i] <= arr[i + 1]);
|
||||||
|
}
|
||||||
|
free(arr);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Driver Code */
|
||||||
int main(int argc, const char *argv[])
|
int main(int argc, const char *argv[])
|
||||||
{
|
{
|
||||||
int n;
|
/* Intializes random number generator */
|
||||||
printf("Enter size of array:\n");
|
srand(time(NULL));
|
||||||
scanf("%d", &n); // E.g. 8
|
test();
|
||||||
|
|
||||||
printf("Enter the elements of the array\n");
|
|
||||||
int i;
|
|
||||||
int *arr = (int *)malloc(n * sizeof(int));
|
|
||||||
for (i = 0; i < n; i++)
|
|
||||||
{
|
|
||||||
scanf("%d", &arr[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("Original array: ");
|
|
||||||
display(arr, n); // Original array : 10 11 9 8 4 7 3 8
|
|
||||||
|
|
||||||
bubbleSort(arr, n);
|
|
||||||
|
|
||||||
printf("Sorted array: ");
|
|
||||||
display(arr, n); // Sorted array : 3 4 7 8 8 9 10 11
|
|
||||||
|
|
||||||
free(arr);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user