integer sort fix

This is a discussion on integer sort fix within the C++ Programming forums, part of the General Programming Boards category; This sort program is not working correctly, I was wondering what was wrong: #include <iostream.h> #include <stdlib.h> int main() { ...

  1. #1
    Unregistered
    Guest

    Question integer sort fix

    This sort program is not working correctly, I was wondering what was wrong:

    #include <iostream.h>
    #include <stdlib.h>

    int main()
    {
    int num[6];
    int temp;

    cout << "Enter 5 numbers to be sorted: ";
    cin >> num[0] >> num[1] >> num[2] >> num[3] >> num[4];
    for(int i=0; i<6; i++){
    if(num[i] >= num[i+1]){

    temp = num[i+1];
    num[i+1] = num[i];
    num[i] = temp;
    }
    }
    for(int j=0; j<5; j++){
    cout << num[j] << " ";
    }

    }

    It sorts it somewhat, but it is not in the correct order(least to greatest). Thanks for the help.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    you are only looping through the array once so each element will only have had one comparison.... you need to add another loop around the outside of the first loop to keep looping all through the array until its sorted. if an array has x elements then this loop needs to run x-1 times!
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    223
    This sort program is not working correctly, I was wondering what was wrong:

    #include <iostream.h>
    #include <stdlib.h>

    int main()
    {
    int num[6];
    int temp;

    cout << "Enter 5 numbers to be sorted: ";
    cin >> num[0] >> num[1] >> num[2] >> num[3] >> num[4];
    ...for(int i=0; i<5; i++){ //changed 6 to 5

    ......for(int k=i+1;k<6;k++) //added inner loop
    ......{

    ..........if(num[i] > num[k]){ //removed = why swap on = ??
    //that is like giving you a dollar in return for a dollar
    //to change the asceding or descending change
    //the comparison symbol from > to <
    .............temp = num[k];
    .............num[k] = num[i];
    .............num[i] = temp;
    ..........}
    .......}
    ..}
    ...for(int j=0; j<5; j++){
    ......cout << num[j] << " ";
    ....}

    ...}

    zMan -- :-) Have a nice day
    zMan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 01:24 PM
  3. Straight Insertion Sort function problem
    By StaticKyle in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2008, 04:03 AM
  4. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 07:38 AM
  5. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 09:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21