0

I read the tutorial regarding arrange a number of array in ascending order and understood the idea https://www.includehelp.com/cpp-programs/sort-an-array-in-ascending-order.aspx . However, now I'm thinking of other way to perform the operation. Wonder will the idea below works?

The method will be using while loop and check (while remaining number in array not equal to 0), find the smallest number in the array, print out the number and remove it from array. Repeat the same process until remaining number in array = 0. So my numbers will be print out in ascending order also and the number in the array will decrease in each loop until it reached zero.

I started learning programming just few weeks ago and have trouble writing out the code now. However I'm interested to know if this method will work? If cannot, please explain why.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
chuackt
  • 145
  • 5
  • Yes, this will work. What makes you think it would not work? But this method is very inefficient. Why don't you just try it? – Jabberwocky Nov 06 '20 at 08:22
  • 2
    That does not look like a good resource to learn from, and I'm highly skeptical you'll learn anything valuable at all from it. At best you'll pick up some awful habits that we'll have to work to undo. I'd suggest finding a [better reference](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – tadman Nov 06 '20 at 08:24
  • Yes it will work but if you want to remove elements from array then you will have to resize/rearrange the array, which will be little costly – csavvy Nov 06 '20 at 08:25
  • Many thank for the efforts in answering. As I still struggling to write out the code, wouldn't know the result if its working. – chuackt Nov 06 '20 at 08:47
  • With a suitable title this question is not so bad. @JerryCoffin Would you like to make an answer? – Yunnosch Nov 06 '20 at 08:47
  • There are many references on sorting algorithms. Knuth Vol 3 is good, though not easy. – Paul Floyd Nov 06 '20 at 09:13

1 Answers1

1

What you've described is a variant of what's normally called a "selection sort". It's pretty well known. It does work, but there are many sorting algorithms that work--and while there are a few sorting algorithms that are generally less efficient, it's still one of the least efficient around.

Selection sort is typically faster than Bubble sort and a few of its variants like Shaker sort. Depending on the precise situation, it can also be faster than insertion sort, though that's pretty unusual. Those three (bubble sort, insertion sort, and selection sort) are the best known of the simple sorting algorithms. Of the three, bubble sort is most often the slowest, and insertion sort most often the fastest. But all three take time proportional to the square of the number of items being sorted, which means they get much slower in a hurry as you try to sort more items. If you have very many items, more advanced algorithms (e.g., Shell-Metzner, Quicksort, heap sort and merge sort) will almost always be substantially faster.

Ignoring execution speed for a moment, selection sort does have one extremely good property: it's easy to understand, easy to code up correctly and easy to prove that it works. If you only need to sort a few items, and need to type in the sorting code yourself (especially if you're in a hurry) it's my experience that it's probably the easiest sorting algorithm to be certain you've implemented correctly.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111