#include <stdio.h> //------TO PRINT THE ARRAY------- void printArray(int *A, int n) { for (int i = 0; i < n; i++) { printf("%d ", A[i]); } printf("\n"); } //-------BUBBLE SORTING THE ARRAY----------- void bubbleSort(int *A, int n) { int temp; int isSorted = 0; for (int i = 0; i < n - 1; i++) { //printf("Working on pass number : %d\n", i + 1); for (int j = 0; j < n - i - 1; j++) { if (A[j] > A[j + 1]) { temp = A[j]; A[j] = A[j + 1]; A[j + 1] = temp; } } } } void BubllesortApdative(int *A, int n) { int temp; int isSorted = 0; for (int i = 0; i < n - 1; i++) { printf("Working on the pass number %d\n", i + 1); isSorted = 1; for (int j = 0; j < n - 1 - i; j++) { if (A[j] > A[j + 1]) { temp = A[j]; A[j] = A[j + 1]; A[j + 1] = temp; isSorted = 0; } } if (isSorted) { return; } } } int main() { int A[] = {1, 23, 34, 32, 45, 5, 78, 9, 45, 43, 34, 4, 56, 67, 6}; int n = 15; printArray(A, n); bubbleSort(A, n); printArray(A, n); return 0; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter