Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- /*
- * Unoptimized Bubble Sort
- * Algorithms Lab CSE 2032.2
- */
- int main()
- {
- int array[100], n, i, j, temp, c;
- printf("Enter the number of elements to sort: ");
- scanf("%d",&n);
- printf("Input the numbers: ");
- for(i=0; i<n; i++)
- {
- scanf("%d",&array[i]);
- }
- bool flag;
- // Bubble Sort (Unoptimized)
- for(i=1; i<=n-1; i++)
- {
- for(j=0; j<n-i-1; j++)
- {
- flag = false;
- if(array[j]>array[j+1])
- {
- //Swap the numbers
- temp = array[j];
- array[j]=array[j+1];
- array[j+1]=temp;
- flag = true;
- }
- }
- if(flag == false ){
- break;
- }
- // Print after each iteration:
- printf("\n\nIteration #%d: ",i);
- for(c=0; c<n; c++)
- {
- printf("%d ",array[c]);
- }
- }
- printf("\n\n\nSorting complete!\n\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement