Thread: Sorting list

  1. #1
    T-Mac wiz23's Avatar
    Join Date
    Apr 2005
    Location
    Houston
    Posts
    48

    Sorting list

    I am currently working on a sorting list and I want to find the min, half and max from the sorted list. I have already sorted the list as follows:

    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    int main()
    {
    	const int arraySize = 7;
    	int a[arraySize] = {7, 77, 2, 4, 1, 9, 3};
    	int i, hold;
    
    	for (i=0; i<arraySize;++i)
    		
    	for (int pass = 0; pass < arraySize-1; ++pass)
    	{
    		for (i=0; i<arraySize-1;++i)
    		{
    			if (a[i] > a[i+1])
    			{
    				hold = a[i];
    				a[i] = a[i+1];
    				a[i+1] = hold;
    			}
    		}
    	}
    	
    	for (i=0; i<arraySize; ++i)
    	{
    		cout <<left
                 << setw(2) << a[i];
    	}
    
    	cout << endl;
    	pause();
            return EXIT_SUCCESS;
    }
    The final output should be: 1 4 77
    Can somebody help me to find the min, half and max. Thankyou
    You can't guard me 1 on 1!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You are sorting in ascending order.
    The minimum is the first element, the maximum is the last element, and the median is the (first+last)/2 element (or thereabouts, since you have to account for array indices starting from 0 and the array possibly having an even number of elements, in which case you may take the mean of the two middle elements).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    6
    i've done the question, so i'll give u a hint on how i did it (using bubble sort

    Code:
        while(i != length-1 )
        {
        
           if(list[i] > list[i+1])
           {
                temp = list[i+1];
                  list[i+1] = list[i];
                  list[i] = temp;
                  i=0;
           }     
            else
            {
             i++;
            }
        }
    
    	min = list[0];
    	max = list[length - 1];
    	half = list[length/2];

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 10:16 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM