Sorting list

This is a discussion on Sorting list within the C++ Programming forums, part of the General Programming Boards category; I am currently working on a sorting list and I want to find the min, half and max from the ...

  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
    21,456
    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).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 01:46 PM
  3. Replies: 6
    Last Post: 03-02-2005, 01:45 AM
  4. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 09:16 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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