How do we select sort an array?

For example if we have an array array[5]={9,1,6,7,3};

How do we sort it in an ascending order?

I'm asking here because I missed this lecture and now my teacher is unavailable. Please tell me how i cant sort it. I'm supposed to sort it using loop(s) and an if or if-else condition.
Sounds like you're looking for the selection sort algorithm. This guy goes through the concept of it, and how to code it step by step.

https://www.youtube.com/watch?v=zHJmHHiqRZw
Last edited on
I know how to bubble sort. As I recall, my teacher told us about three types of sorting; bubble sort, selection sort and insertion sort. He hasn't taught the the class insertion sort yet.
This is my bubble sort program;
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
#include <iostream>
using namespace std;
int main()
{
	const int size=10;
	int array[size]={75,8,36,32,15,45,0,18,25,20};
	int temp=0;

	for(int repeat = 1 ; repeat < size ; repeat++)
	{
		for(int arr_index=0 ; arr_index < size - repeat ; arr_index++)
		{
			if(array[arr_index] > array[arr_index+1])
			{
				temp = array[arr_index];
				array[arr_index] = array[arr_index+1];
				array[arr_index+1] = temp;
			}
		}	
	}
		
	for(int index=0;index<10;index++)
	{
		cout<<array[index]<<" ";
	}
	return 0;
}

My teacher hasn't taught the class to how to use the swap function and we are not supposed to use it even if we know how to use it. We are supposed to store the number in a different variable like i have done in the bubble sort above.

Oh and whats the difference between bubble sort and selection sort?
If you take a look at the article, it should describe what a selection sort is.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int seek, minCount, minValue;
    for (seek = 0; seek < (elems-1);seek++)
    {
        minCount = seek;
        minValue = array[seek];
        for(int index = seek + 1; index < elems; index++)
        {
            if(array[index] < minValue)
            {
                minValue = array[index];
                minCount = index;
            }
        }
        array[minCount] = array[seek];
        array[seek] = minValue;
    }
Topic archived. No new replies allowed.