selection sort

selection sort using an Array

Page 1 of 1

1 Replies - 2045 Views - Last Post: 07 May 2008 - 09:27 PM Rate Topic: -----

#1 cooter1341  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-April 08

selection sort

Posted 07 May 2008 - 06:40 PM

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


#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: 07 May 2008 - 06:44 PM


Is This A Good Question/Topic? 0
  • +

Replies To: selection sort

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4402
  • View blog
  • Posts: 12,257
  • Joined: 18-April 07

Re: selection sort

Posted 07 May 2008 - 09:27 PM

Well since you know that you are going to enter a specific number of items you can cut down all the repetitive prompting with something like...

#define SIZE 12

int main() {
     int a[SIZE];

     cout << "We are going to enter " << SIZE << " numbers..." << endl;

     // Loop through SIZE times and set each element of the array to a number.
     for (int i = 0; i < SIZE; i++) {
          cout << "Enter number " << (i + 1) << ": ";
          cin >> a[i];
     }

     // Rest of code here



That should shorten your prompting and initializing code up quite a bit. So look it through and try it out.

Enjoy!

"At DIC we be code shortening code ninjas... some guys are just shorter than others *ahem axel*" :snap:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1