#include <iostream>
using namespace std;
int main()
{
int integers,swap;
int array[10];
cout << "Enter ten unsorted integers..." << endl;
for (int i=0;i<10;i++)
{
cout << "[" << i << "] = ";
cin >> integers;
array=integers;
}
cout << "Unsorted List = ";
for (int j=0; j<10; j++)
{
cout << array[j] << ", " ;
}
cout << endl;
cout << "Sorting..." << endl;
cout << "Sorted List = ";
for(int k=0;k<10;k++)
{
if(array[k+1] < array[k])
{
swap=array[k+1];
array[k+1]=array[k];
array[k]=swap;
}
cout << array[k] << ", ";
}
cout << endl;
return 0;
}
selection sort
I am trying to program a selection sort routine, here is my code
Bubble sort; you need to iterate through the array until all elements satisfy arrayN < arrayN+1.
EDIT:
Ninja'd x3. Look at your for-loop code carefully, if array has elements indexed from 0 to 9, then this loop:
What is k+1 when k is 9?
EDIT:
Ninja'd x3. Look at your for-loop code carefully, if array has elements indexed from 0 to 9, then this loop:
for(int k=0;k<10;k++) if(array[k+1] < array[k])
What is k+1 when k is 9?
Looks more like bubble sort to me.
You're only making one pass. You need to keep doing your swap loop until you don't detect any swaps.
You're only making one pass. You need to keep doing your swap loop until you don't detect any swaps.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement