Array Sorting problem

This is a discussion on Array Sorting problem within the C++ Programming forums, part of the General Programming Boards category; The program takes the input and finds mean, median, min, and max of the input numbers. I am having problems ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    11

    [solved] Array Sorting problem

    The program takes the input and finds mean, median, min, and max of the input numbers. I am having problems sorting an array. When it comes to sorting, the largest number gets put first, when in fact it should be smallest to largest. After the first number, the rest of the numbers are correctly sorted. Compiled with Microsoft Visual C++ 6.0.

    Code:
    //AS-30
    #include <iostream>
    #include <string>
    using namespace std;
    
    void main ()
    {
      double data [20],sdata[20];
      int length,i;
      double min,max,mean,median,sum,temp;
      bool swapped;
      //input data into array
      cout <<"Enter data size: "<<endl;
      cin >>length;
    
      for (i=0; i<length;i++)
      {
        cout <<"Enter a data item: "<<endl;
        cin >>data[i];
      }
      //copy the contents of data into sdata
      
        for (i=0; i<length;i++)
      {
        sdata[i] = data[i];
      }
      //sort array sdata
      swapped=true;
      while (swapped)
        {
        swapped=false;
        for (i=0;i<length;i++)
        {
          if (sdata[i] > sdata[i+1])
         {
          //swap
          temp=sdata[i];
          sdata[i] = sdata [i+1];
          sdata [i+1] = temp;
          swapped=true;
          }
        }
      }
      //compute min,max,mean,median
        min=sdata[0];
        max=sdata[length-1];
        sum=0;
    
       for (i=0; i<length;i++)
      {
         sum=sum+sdata[i];
      }
      mean=sum/length;
    
      int indexHi,indexLo,index;
      if ((length %2) != 0 )
      {
        index = length / 2;
        median = sdata [index];
      }
      else 
      {
        indexHi = length / 2;
        indexLo = indexHi -1;
        median = (sdata[indexLo] + sdata[indexHi] ) / 2;
      }
    
      //display original data
      cout <<"Original data: "<<endl;
      for (i=0; i<length;i++)
      {
        cout <<data[i] << " ";
      }
      cout <<endl;
      //display sorted data
      cout <<"Sorted data: "<<endl;
      for (i=0; i<length;i++)
      {
        cout <<sdata[i] << " ";
      }
      cout <<endl;
      cout <<"Min: "<<min<<endl;
      cout <<"Max: "<<max<<endl;
      cout <<"Mean: "<<mean<<endl;
      cout <<"Median: "<<median<<endl;
    }
    Last edited by ___________; 07-22-2008 at 04:31 PM.

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Code:
    for (i=0;i<length;i++)
    You're overrunning your array... only loop through length - 1 (since you have i+1 in the loop).

    Also, there are more efficient sort functions out there, and they're given to you. Search the web for how to use std::sort, and (for future reference) std::swap. (std::sort will be better than your sort, std::swap will be equal or better depending on the data type.)

    Edit: hmm, std::copy might also have helped you... algorithms. This is what makes C++ so much fun a lot of the time... the basic stuff is done for you.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Like cactus_hugger said, some algorithms are already done for you in C++ standards. Your sorting algorithm is/is a form of bubble sorting. Selection sorting is faster, as it only needs to go through the entire array one complete time for every number in the array. Bubble sorting on the other hand, goes through the entire array as many times as it needs in order to make the array sorted. I had to do a comparison of the two for an assignment and it took bubble sorting roughly ten times more "cycles" to go through the same numbers (different numbers will be a different result) that selection sort could do.

  4. #4
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Maybe the bubble sort is the assignment. Not some Binary Tree Sorting or such. Anyway, what Cactus_Hugger said is correct. You have this line:
    Code:
    if (sdata[i] > sdata[i+1])
    So, if it reached this point where i= length - 1, the "sdata[i+1]" will be equal to "sdata[i+1]" and will return a garbage value because it's out of the array bound. At a time like this, the Step Over and Step Into feature of a debugger will come in handy.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,301
    Quote Originally Posted by scwizzo View Post
    Like cactus_hugger said, some algorithms are already done for you in C++ standards. Your sorting algorithm is/is a form of bubble sorting. Selection sorting is faster, as it only needs to go through the entire array one complete time for every number in the array. Bubble sorting on the other hand, goes through the entire array as many times as it needs in order to make the array sorted. I had to do a comparison of the two for an assignment and it took bubble sorting roughly ten times more "cycles" to go through the same numbers (different numbers will be a different result) that selection sort could do.
    Selection sort is typically faster, but not for the reason you stated. "as many times as it needs in order to make the array sorted" can easily be less than "one complete time for every number in the array".
    In fact, an optimised bubble sort never has to perform more passes than a Selection sort, and can often perform fewer. The sole reason selection sort is usually faster is because it does less copying of the objects it is sorting. However, I would only expect a 10x speed difference for an object type that is quite expensive to copy.

    OP: You've made the mistake of writing void main, that is not valid C or C++ code. It MUST be declared as returning an int.
    You are also missing data integrity checks. What's stopping someone from saying they want to input 999999 numbers, and crashing the program?
    Last edited by iMalc; 07-22-2008 at 12:20 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Problem sorting an array of strings
    By Leojeen in forum C Programming
    Replies: 7
    Last Post: 05-07-2008, 09:02 PM
  3. Problem with copying a string into array in a struct
    By JFonseka in forum C Programming
    Replies: 15
    Last Post: 05-04-2008, 05:07 AM
  4. Problem with file and array
    By paok in forum C Programming
    Replies: 5
    Last Post: 05-01-2008, 04:19 AM
  5. 3D array problem
    By rainman39393 in forum C++ Programming
    Replies: 1
    Last Post: 03-15-2008, 06:09 PM

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