-3

just wanted to know if the below is correct. I am trying to solve this using the insertion sort algorithm. I have tried to attempt the below array and have come up with these answers. Please could you let me know if this is correct, and if i have the correct understanding. Many Thanks.

Reverse sorted sequence:

15, 12, 10, 4

12, 15, 10, 4

12, 10, 15, 4

12, 10, 4, 15

10, 12, 4, 15

10, 4, 12, 15

10, 4, 12, 15 (no swap)

4, 10, 12, 15
Andy
  • 49,085
  • 60
  • 166
  • 233
Abdul
  • 15
  • 6
  • 2
    Why do you think that you should have that "no swap" line? And why did you tag this with java, when you are only asking us to review if the output of your implementation is correct? – GhostCat Nov 20 '17 at 11:49
  • What is your concrete question. You posted a similar question before: https://stackoverflow.com/questions/47310732/selection-sort-algorithm-using-small-array . If you only need a valid example for the sorting algorithm, I'm sure you can find plenty with the search- engine of you choice. – MrSmith42 Nov 20 '17 at 11:50
  • I think this looks ok. (The "no swap" line is there to pinpoint the fact that 4 and 12 are already in the right order.) But don't tag this with java, this is not a java question. – Stefan Nov 20 '17 at 11:51
  • @Stefan this is not ok. 3rd and 4th(4 should not be moved at that time)steps are wrong and because of it, this has same complexity as selection sort in each case – Luai Ghunim Nov 20 '17 at 12:27

2 Answers2

0

The sequence with comments:

15, 12, 10,  4    start
12, 15, 10,  4    1st outer loop  [0:1]
12, 10, 15,  4    2nd outer loop  [1:2]
10, 12, 15,  4                    [0:1]
10, 12,  4, 15    3rd outer loop  [2:3]
10,  4, 12, 15                    [1:2]
 4, 10, 12, 15                    [0:1]
rcgldr
  • 27,407
  • 3
  • 36
  • 61
0

As stated in LECTURE NOTES ON DATA STRUCTURE on page 176

Suppose, you want to sort elements in ascending as in above figure. Then,

Step 1: The second element of an array is compared with the elements that appears before it (only first element in this case). If the second element is smaller than first element, second element is inserted in the position of first element. After first step, first two elements of an array will be sorted.

Step 2: The third element of an array is compared with the elements that appears before it (first and second element). If third element is smaller than first element, it is inserted in the position of first element. If third element is larger than first element but, smaller than second element, it is inserted in the position of second element. If third element is larger than both the elements, it is kept in the position as it is. After second step, first three elements of an array will be sorted.

Step 3: Similary, the fourth element of an array is compared with the elements that appears before it (first, second and third element) and the same procedure is applied and that element is inserted in the proper position. After third step, first four elements of an array will be sorted.

bummi
  • 27,123
  • 14
  • 62
  • 101
Abdul
  • 15
  • 6