Why won't my selection sort, sort the code

My code is remaining unsorted when i run the program? Also where would i prompt the user to choose a number after or q to quit? -- line 57
I know i need a swap(), but i'm not sure how to implement it, i kept getting errors, a small explanation would be great!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
  #include <iostream>
#include <fstream>
#include <string>
#include <algorithm> // for std::swap, use <utility> instead if C++11
#include <iterator>

using namespace std;

const int SIZE = 200;
int numbers[SIZE];
int count = 0;

int binarySearch(int array[], int size, int value);
void selectionSort(int array[], int size);



int main()
{
    // prompt user to enter file name. also display if it exists or not
    ifstream inputFile; // gives access to file
    string name, filename;
    int number;

    // Get file name from the user
    cout << "Enter the file name: ";
    cin >> filename;

    // Open the file
    inputFile.open(filename.c_str());

    // If the file opened successfully, process it
    if (inputFile)
    {
        // Read the numbers from the file and display them.
        while (inputFile >> number)
        {
            cout << number << endl;
        }

        // Close the file
        inputFile.close();
    }
    else
    {
        // display an error message if the file was not found
        cout << "Error opening the file.\n";

    }

   return 0;
}


    // prompt the user to enter a # to search the array or Q to quit

    // sort the array using selection sort
    void selectionSort(int array[], int size) // ADD THE COMMENTS BELOW
    {

        int startScan, minIndex, minValue; // declaration

        for(startScan = 0; startScan < (size - 1); startScan++) // search begins from one element below the actual starting point
        {
            minIndex = startScan;
            minValue = array[startScan];
            for(int index = startScan + 1; index < size; index++) // incremented by one value at a time
            {
                if(array[index] < minValue)
                {
                    minValue = array[index]; // locates element in position 0
                    minIndex = index;
                }
            }
            array[minIndex] = array[startScan];
            array[startScan] = minValue;

        }
    }
Last edited on
I tried adjusting it with that swap but ir still doesn't sort it and just closes the program after displaying the numbers unsorted.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 void selectionSort(int array[], int size) //
    {

        int startScan, minIndex, minValue; // declaration
        bool swap;

        do
        {
         swap = false;

        for(startScan = 0; startScan < (size - 1); startScan++) // search begins from one element below the actual starting point
         {
            minIndex = startScan;
            minValue = array[startScan];
            for(int index = startScan + 1; index < size; index++) // incremented by one value at a time
            {
                if(array[index] < minValue)
                {
                    minValue = array[index]; // locates element in position 0
                    minIndex = index;
                    swap = true;
                }
            }
            array[minIndex] = array[startScan];
            array[startScan] = minValue;

         }

        } while(swap);

    }
you are not even calling the function, ¿how do you expect that to work?

> I know i need a swap(), but i'm not sure how to implement it
std::swap(array[minIndex], array[startScan]);
1
2
3
4
def swap(a, b): //pseudocode
   aux = a
   a = b
   b = a b = aux //thanks, doug4 



try to understand the logic of the algorithm before coding (it's like some pages of your cookbook are stuck together)
in selection sort you have a bag of elements, at each step you extract the minimum element from the bag and so you obtain a sorted sequence
1
2
3
4
5
6
7
def selection_sort(bag):
   result = []
   while not is_empy?(bag):
      min = min_element(bag) //extract the minimum element
      bag.erase(min)
      result = [result, min] //storing the sorted sequence
   return result
so in your first code, lines 67--74 search for the minimum element and lines 75--76 is where you should extract that element from the bag and insert it into the sorted sequence (it's using the same container in this case, if that confuses you just create another array)
Last edited on
Why would you think it should do anything other than display some numbers (if the file opened correctly)?

The only thing you're doing is printing out the numbers from the file.

You never store the numbers in an array, so there is nothing to sort.

You also never call your sort function so it really doesn't matter that you don't have an array.

@ne555 has a typo in his swap pseudocode. The 4th line should be b = aux, not b = a
Your original code works find if you (1) actually call selectionSort; (2) print the array after sorting it, and (3) move int count; inside main so it doesn't conflict with std::count() in algorithm.

Point #3 demonstrates why it's a bad idea to add using namespace std; to your code. It's better to add using statements (directives? They aren't technically statements) for each symbol that you'll use.
Topic archived. No new replies allowed.