Click here to Skip to main content
15,041,264 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Suppose we want to put an array of n integer numbers into ascending numerical order. This 
task is called sorting. One simple algorithm for sorting is selection sort. You let an index i go 
from 0 to n-1, exchanging the ith element of the array with the minimum element from i up to n. 
Using this finite set of integers as the input array {4 3 9 6 1 7 0}:
i. Perform the asymptotic and worst-case analysis on the sorting algorithm been 
ii. Write or draw all the iterations in the selection sort process on the array. 
iii. Write a java or C++ code implementing the algorithm. 

What I have tried:

i try coding it in c++ to sort the numbers and also do the drawing. Please i need your assistance. Thanks in advance
Updated 28-Apr-21 16:50pm
Patrice T 22-Apr-21 4:08am
Show your work

Try harder: It is quite simple. An array and two for loops will do the trick.
See, for instance: Arrays - C++ Tutorials[^] and C++ for loop - Tutorialspoint[^].
Maciej Los 22-Apr-21 2:22am
CPallini 22-Apr-21 2:25am
Thank you!
We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.
As CPallini says, this isn't difficult - just think about how you would do it manually, and it should be pretty obvious how to use two nested for loops to do it.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
index	0	1	2	3	4	5	6	comments
---------------------------------------------------------	--------
    |	4	3	9	6	1	7	0	initial
i=0 |	0	3	9	6	1	7	4	swap 0,4
i=1 |	0	1	9	6	3	7	4	swap 1,3
i=2 |	0	1	3	6	9	7	4	swap 3, 9
i=3 |	0	1	3	4	9	7	6	swap 6, 4
i=4 |	0	1	3	4	6	7	9	swap 9, 6
i=5 |	0	1	3	4	6	7	9	(done)

int find_min_index (int [] n);
void swap (int [] n);

void selection_sort (int v[], int n) {
	int	i;

	for (i=0; i

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900