Sorting function not working

This is a discussion on Sorting function not working within the C++ Programming forums, part of the General Programming Boards category; Why is my sorting function not working? Code: void sort(int array[], int numts) { int lowIndex, lowest; for (int count ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    22

    Sorting function not working

    Why is my sorting function not working?

    Code:
    void sort(int array[], int numts)
    {
    	int lowIndex, lowest;
    
    	for (int count = 0; count < (numts-1); count++)
    	{
    		lowIndex = count;
    		lowest = array[count];
    		for (int index = count + 1; index < numts; index++)
    		{
    			if (array[index] < lowest)
    			{
    				lowest = array[index];
    				lowIndex = index;
    			}
    		}
    		array[lowIndex] = array[count];
    		array[count] = lowest;
    
    		lowest = array[count];
    		array[count] = array[lowIndex];
    		array[lowIndex] = lowest;
    	}
    	return;
    }
    I am trying everything!

  2. #2
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by musique View Post

    Code:
    		array[count] = lowest;
    
    		lowest = array[count];
    I doubt that does what you wanted it to do
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  3. #3
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by musique View Post
    Why is my sorting function not working?

    Code:
    void sort(int array[], int numts)
    {
    	int lowIndex, lowest;
    
    	for (int count = 0; count < (numts-1); count++)
    	{
    		lowIndex = count;
    		lowest = array[count];
    		for (int index = count + 1; index < numts; index++)
    		{
    			if (array[index] < lowest)
    			{
    				lowest = array[index];
    				lowIndex = index;
    			}		}
    		array[lowIndex] = array[count];
    		array[count] = lowest;
    
    		lowest = array[count];
    		array[count] = array[lowIndex];
    		array[lowIndex] = lowest;
    	}
    	return;
    }
    I am trying everything!
    I think you need to swap the elements here using a third variable.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    		lowest = array[count];
    		array[count] = array[lowIndex];
    		array[lowIndex] = lowest;
    It could be that this part is superfluous.

    This should be called Selection Sort. At each iteration you find the lowest value in the remaining range and swap that with the first item in the range.

    It seems to me that this already does the last part:
    Code:
    		array[lowIndex] = array[count];
    		array[count] = lowest;
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    22
    Thanks for the reply ben10 but what do you mean? As in array [count]?

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    22
    Thank you for the reply anon. I tried it but it didn't work. Should incorporate pointers?

  7. #7
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by musique View Post
    Thanks for the reply ben10 but what do you mean? As in array [count]?
    If you find a number to be greater than the previous one you don't do anything but if the reverse happens i.e if you find a number which is smaller than the previous number, you have to swap them. And swapping is generally done using a third variable.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,293
    Quote Originally Posted by BEN10 View Post
    If you find a number to be greater than the previous one you don't do anything but if the reverse happens i.e if you find a number which is smaller than the previous number, you have to swap them. And swapping is generally done using a third variable.
    That's because he's implementing selection sort, not bubble sort.
    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"

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    And swapping is generally done using a third variable.
    But you see, there is a third variable there, and it is called lowest. (Both you and the OP may have a problem getting "attached" to certain patterns in the code and not recognizing their variations )

    Thank you for the reply anon. I tried it but it didn't work. Should incorporate pointers?
    I don't understand. I just utilized the delete key and it seems to be working to me.

    Code:
    void sort(int array[], int numts)
    {
        int lowIndex, lowest;
    
        for (int count = 0; count < (numts-1); count++)
        {
            lowIndex = count;
            lowest = array[count];
            for (int index = count + 1; index < numts; index++)
            {
                if (array[index] < lowest)
                {
                    lowest = array[index];
                    lowIndex = index;
                }
            }
            array[lowIndex] = array[count];
            array[count] = lowest;
        }
        return;
    }
    
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <iterator>
    
    int main()
    {
        std::vector<int> vec;
        std::copy(std::istream_iterator<int>(std::cin), std::istream_iterator<int>(), std::back_inserter(vec));
        sort(&vec[0], vec.size());
        std::copy(vec.begin(), vec.end(), std::ostream_iterator<int>(std::cout, " "));
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  10. #10
    Registered User
    Join Date
    Apr 2009
    Posts
    22
    Yup, I just tried moving the show array function to see if it just was not displaying and it was just that, it is working just was not being displayed, thanks. I just need to add pointers to it now, but when I do that all hell breaks lose and I get this error (error C2664: 'showArray' : cannot convert parameter 1 from 'int *' to 'int *[]')...example...

    Code:
    /***This program dynamically allocates an array large enough to
    hold a user-defined number of test scores***/
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    //Function prototypes
    int getnumtestscores();
    void gettestscores(int[], int);
    void showArray(int *[], int);
    void sort(int [], int);
    double average(int[], int);
    
    
    int main()
    {
    	int numts = getnumtestscores();
    
    	int *testscores = new int[numts];		//To dynamically allocate an array
    
    	gettestscores(testscores, numts);
    
    	cout << "The testscores are: \n"; showArray (testscores, numts);
    
    	cout << "The testscores sorted are: \n"; sort (testscores, numts);
    
    	showArray (testscores, numts);
    
        cout << "The average of all " << numts << " test scores is: " 
             << fixed << showpoint << setprecision(2)
             << average(testscores, numts);
    	cout << endl;
    
        // Free dynamically allocated array in memory
        delete [] testscores;
        // Make scores point to null
        testscores = 0;
    
    	return 0;
    }
    
    int getnumtestscores()
    {
        int num;
    
        cout << "\n How many test scores do you want to enter : ";
        for(;;) // loop forever  ... until return
        { 
            cin >> num;
         
            return num;
        }  
    }
    
    void gettestscores(int testscores[], int numts)
    {
    	cout << "What are the testscores for the students?\n";
    
    	for (int count = 0; count < numts; count++)
    	{
    		cout << "Testscore " << setw(2) << (count+1) << ":";
    
    		if (testscores < 0)		//Input validation
    		{
    			cout << "An invalid score was entered, please enter a score more than 0\n";
    		}
    
    		cin >> testscores[count];
    	}
    }
    
    void sort(int *array[], int numts)
    {
    	int lowIndex, *lowest;
    
    	for (int count = 0; count < (numts-1); count++)
    	{
    		lowIndex = count;
    		lowest = array[count];
    		
    		for (int index = count + 1; index < numts; index++)
    		{
    			if (*(array[index]) < *lowest)
    			{
    				lowest = array[index];
    				lowIndex = index;
    			}
    		}
    		array[lowIndex] = array[count];
    		array[count] = lowest;
    
    
    	}
    
    	return;
    }
    
    void showArray(int array[], int numts)
    {
    	for (int count = 0; count < numts; count++)
    		cout << array[count] <<" \n";
    	cout << endl;
    }
    
    double average(int array[], int numts)
    {
    	int total = 0;
    
    	for (int count = 0; count < numts; count++)
    	
    		total +=array[count];
    
    	  return total/numts;
    }

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,293
    Are you wanting to sort an array of pointers to things, or are you wanting to use a pointer to an array? (The later being totally unnecessary since the array is a pointer when passed to sort).
    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"

  12. #12
    Registered User
    Join Date
    Apr 2009
    Posts
    22
    I was told in order to understand pointers I should try to use it instead of arrays. I don't understand pointers so in order to see how they function I was trying to alter my currently working program with pointers. That is what I want to see...how the pointers are exactly the same as the array.

  13. #13
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    void showArray(int array[], int numts)
    void showArray(int* array, int numts)
    These two are the same. When you pass an array to a function, it becomes a pointer to the first element.

    Code:
    cout << array[count] <<" \n";
    cout << *(array + count) << '\n';
    These two are also the same. The [] operator is just short-hand for pointer arithmetic. Although in a more pointer-oriented approach you'd keep incrementing it, instead of adding some offset to each access:

    Code:
    void showArray(int* array, int numts)
    {
        int* end = array + numts;
        for (; array != end; ++array)
            cout << *array <<" \n";
        cout << endl;
    }
    Last edited by anon; 05-03-2009 at 04:41 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  14. #14
    Registered User
    Join Date
    Apr 2009
    Posts
    22
    Hmmm, let me try it and see, thanks for your help anon.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

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