Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 131,646 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,727 people online right now. Registration is fast and FREE... Join Now!




selection sort

 
Reply to this topicStart new topic

selection sort, selection sort using an Array

cooter1341
post 7 May, 2008 - 05:40 PM
Post #1


New D.I.C Head

*
Joined: 23 Apr, 2008
Posts: 6

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
User is offlineProfile CardPM

Go to the top of the page


Martyr2
post 7 May, 2008 - 08:27 PM
Post #2


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,008



Thanked 170 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


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...

cpp

#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*" decap.gif
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/20/08 06:16AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month