Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. #include <stdio.h>
  2. /*
  3. * Unoptimized Bubble Sort
  4. * Algorithms Lab CSE 2032.2
  5. */
  6. int main()
  7. {
  8. int array[100], n, i, j, temp, c;
  9.  
  10. printf("Enter the number of elements to sort: ");
  11. scanf("%d",&n);
  12.  
  13. printf("Input the numbers: ");
  14. for(i=0; i<n; i++)
  15. {
  16. scanf("%d",&array[i]);
  17. }
  18. bool flag;
  19. // Bubble Sort (Unoptimized)
  20. for(i=1; i<=n-1; i++)
  21. {
  22. for(j=0; j<n-i-1; j++)
  23. {
  24. flag = false;
  25. if(array[j]>array[j+1])
  26. {
  27. //Swap the numbers
  28. temp = array[j];
  29. array[j]=array[j+1];
  30. array[j+1]=temp;
  31. flag = true;
  32. }
  33.  
  34.  
  35. }
  36. if(flag == false ){
  37. break;
  38. }
  39. // Print after each iteration:
  40. printf("\n\nIteration #%d: ",i);
  41. for(c=0; c<n; c++)
  42. {
  43. printf("%d ",array[c]);
  44. }
  45. }
  46. printf("\n\n\nSorting complete!\n\n");
  47.  
  48. return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement