Demo entry 6787223

Bubble Sort technique algorithm

   

Submitted by sumit on Apr 10, 2019 at 16:29
Language: C. Code size: 880 Bytes.

#include <stdio.h>
int main()
{
      int n, i, j, arr[20];
      printf("Enter the number of elements: ");
      scanf("%d",&n);

      for (i = 0; i < n; i++)
      {
            printf("element %d: ",i+1);
            scanf("%d",&arr[i]);
      }
      printf("\nYou Entered:\n\n");
      for (j = 0; j < n; j++)
      {
            printf("%d  ",arr[j]);
      }
      int swap;
      for (i = 0 ; i < n - 1; i++)
      {
            for (j = 0 ; j < (n-i-1); j++)
            {
                  if (arr[j] > arr[j+1])
                  {
                        swap = arr[j];
                        arr[j] = arr[j+1];
                        arr[j+1] = swap;
                  }
            }
      }
      printf("\n\nSorted list in ascending order:\n\n");
      for (i = 0; i < n; i++)
      printf("%d   ", arr[i]);
      return 0;
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).