I have been working on a sorting algorithm (selection sort) using an Array. Here is what I have come up with. It is by no means pretty but it does compile, which is a huge plus.
I was just wondering how I could go about cleaning this up. I know that it is extremely ugly and long. What should I do in order to shorten it up, and what can I afford to get rid of? I know I probably used way to much info or have stuff that does not really belong, so any help would be much appreciated. Thank you
CODE
#include<iostream>
#include<iomanip>
using namespace std;
void selectionSort(int*const,const int);
void swap(int*const,int*const);
void selectionSort(int*const array, const int size){
int smallest;
//loop over size-1 elements
for(int i=0;i<size-1;i++){
smallest=i;
//loop to find index of smallest element
for(int index=i+1;index<size;index++)
if(array[index]<array[smallest])
smallest=index;
swap(&array[i],&array[smallest]);
}
}
#define SIZE 12
int main()
{
int b, c, d, e, f, g, h, j, k, l, m, p;
cout << "We will enter 12 positive numbers : ";
cout << "Enter the first number : ";
cin >> b;
cout << "Enter the second number : ";
cin >> c;
cout << "Enter the third number : ";
cin >> d;
cout << "Enter the fourth number : ";
cin >> e;
cout << "Enter the fifth number : ";
cin >> f;
cout << "Enter the sixth number : ";
cin >> g;
cout << "Enter the seventh number : ";
cin >> h;
cout << "Enter the eighth number : ";
cin >> j;
cout << "Enter the ninth number : ";
cin >> k;
cout << "Enter the tenth number : ";
cin >> l;
cout << "Enter the eleventh number : ";
cin >> m;
cout << "Enter the twelvth number : ";
cin >> p;
int a[SIZE] = {b, c, d, e, f, g, h, j, k, l, m, p};
cout<<"Numbers in the order you entered them\n";
for(int i=0;i<SIZE;i++)
cout<<setw(4)<<a[i];
selectionSort(a,SIZE);
cout<<"\nNumbers in ascending order\n";
for (int j=0;j<SIZE;j++)
cout<<setw(4)<<a[j];
cout<<endl;
return 0;
}
void swap(int*const element1Ptr,int*const element2Ptr){
int hold=*element1Ptr;
*element1Ptr=*element2Ptr;
*element2Ptr=hold;
}
This post has been edited by cooter1341: 7 May, 2008 - 05:44 PM