Thread: 'Sorting' algorithm

  1. #1
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802

    'Sorting' algorithm

    Code:
    #include <iostream.h>
    int main()
    {
    	int num;	
    	cout << "Hey buddy how many numbers do you wanto enter?";
    	cin >> num;
    	cout << "\n";
    	float biggest;
    
    	float *arguments = new float[num];
    	
    	for (int i=0; i<num; i++)
    	{
    		cout << "Enter some numbers from my crazy algorithm to sort ->";
    		cin>>arguments[i];
    		cout << "\n";
    	}
    	for (i=arguments[i-1]; i>0; i--)
    	{
    		for (int j=0; j<num; j++)
    		{
    			if (arguments[j] > arguments[i])
    			{
    				biggest = arguments[j];
    			}
    			else
    			{
    				biggest = arguments[i];
    			}
    		}
    	}
    	cout << "The Biggest number was: " << biggest;
    	int stop;
    	cin>>stop;
    
    	return 0;
    }
    There's a small problem with this sorting algorithm i dished up, based somewhat on bubblesort, although it doesn't work..

  2. #2
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    Use the quick sort...its much faster.
    My Website

    "Circular logic is good because it is."

  3. #3
    Duck-Billed Platypus
    Guest
    eh?

    >for (i=arguments[i-1]; i>0; i--)

    so, your basing how many times you are going to loop on the value of arguments[i-1] ?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    for (i=num-1 ; i>0; i--)
    for (int j=0; j<i; j++)

    > if (arguments[j] > arguments[i])
    At this point, you need to be swapping these two values, not just remembering which one is biggest.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. *Help*in sorting and searching algorithm
    By yuentong in forum C++ Programming
    Replies: 1
    Last Post: 03-07-2009, 10:43 AM
  2. Efficient Sorting Algorithm
    By GCNDoug in forum C++ Programming
    Replies: 10
    Last Post: 11-13-2008, 09:06 AM
  3. Sorting Algorithm Help
    By cjwenigma in forum C++ Programming
    Replies: 8
    Last Post: 11-02-2007, 02:04 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. which sorting algorithm for GBs of data?
    By Sargnagel in forum C Programming
    Replies: 4
    Last Post: 06-05-2003, 08:43 AM