Program B. Develop a C++ program that will sort a one-dimensional integer array
ID: 3817040 • Letter: P
Question
Program B. Develop a C++ program that will sort a one-dimensional integer array LIST of dimension N. You can try using the selection sort algorithm (simple to write but slow), or the bubble sort algorithm (a little more complex, but faster). In testing your program, it is interesting to note the change in behavior of the sort function (number of iterations) when you change the way your data is organized. Try arranging the data so that it is "completely" out of order, and then try the sort on a list that is almost sorted (i.e. only one or two elements are out of order). Note the number of iterations in each case for the different sort algorithms. Can you develop (or find) an even faster algorithm than selection or bubble? please use a code that works on X-Code. thank you
Explanation / Answer
#include<iostream>
using namespace std;
int main()
{
int a[50],n,check;
cout<<"Enter the size of the array: ";
cin>>N;
cout<<"Enter the elements of the array: ";
for(int i=0;i<n;++i)
cin>>a[i];
for(int i=1;i<n;++i)
{
for(int j=0;j<(n-i);++j)
if(a[j]>a[j+1])
{
check=a[j];
a[j]=a[j+1];
a[j+1]=check;
}
}
cout<<"Array after bubble sort:";
for(int i=0;i<n;++i)
cout<<" "<<a[i];
return 0;
}
We have an algorithms like merge sort and quick sort that are faster than bubble sort in most of the cases.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.