Updated Sorting Algorithms With Image
Updated Sorting Algorithms With Image
For example,
Given array [10, 10, 5, 2] becomes [2, 5, 10, 10] after sorting
in increasing order.
Now it works:
1. We sort the array using multiple passes. After the first
pass, the max element goes to end (it correct position).
Same way, after second pass, the second largest element
goes to second last position and so on.
#include <stdio.h>
#include <conio.h>
void bubble sort (int arr[], int n)
{
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (arr[j] > arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
Output:
Original array:{84,32,3,12}
Sorted array(Bubble Sort):{3,12,32,84}