Answer to Question #261085 in C for Shadow

Question #261085

Write a program/method to compare two neighboring numbers in an array when the first index is given.Now combine the above two methods and implement bubble sort algorithm. You must call the above two methods in your sorting program.Now test your sorting program with a random number array generator you created 2


1
Expert's answer
2021-11-05T10:51:40-0400

Pseudocode

Procedure bubble_sort (array , N)
  array – list of items to be sorted
  N – size of array
begin
  swapped = false
  repeat
    for I = 1 to N-1 
      if array[i-1] > array[i] then
        swap array[i-1] and array[i]
        swapped = true
      end if
    end for
  until not swapped
end procedure

Cpp

#include<iostream>
using namespace std;
int main ()
{
   int i, j,temp,pass=0;
   int a[10] = {10,2,0,14,43,25,18,1,5,45};
   cout <<"Input list ...\n";
   for(i = 0; i<10; i++) {
      cout <<a[i]<<"\t";
   }
cout<<endl;
for(i = 0; i<10; i++) {
   for(j = i+1; j<10; j++)
   {
      if(a[j] < a[i]) {
         temp = a[i];
         a[i] = a[j];
         a[j] = temp;
      }
   }
pass++;
}
cout <<"Sorted Element List ...\n";
for(i = 0; i<10; i++) {
   cout <<a[i]<<"\t";
}
cout<<"\nNumber of passes taken to sort the list:"<<pass<<endl;
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS