简体   繁体   中英

Is this a Selection sort or Insertion sort?

Suppose we are sorting an array of ten integers using a some quadratic sorting algorithm. After four iterations of the algorithm's main loop, the array elements are ordered as shown here:

1 2 3 4 5 0 6 7 8 9

Which statement is correct ? (Note: Our selection sort picks largest items first.)

A . The algorithm might be either selection sort or insertion sort.

B . The algorithm might be selectionsort, but could not be insertionsort.

C . The algorithm might be insertionsort, but could not be selectionsort.

D . The algorithm is neither selectionsort nor insertionsort.

I think the answer should be A(both Insertion sort and Selection Sort)? But I found on some websites that correct answer is C. I don't know the reason. Could someone please explain me.
Correct me if I am wrong.

Source : Google Book

Insertion sort is possible since the current snapshot just finished sorting the array from index 0 up to 4.

Selection sort will pick the minimum value (0 in your example) first and put it to the left end. So the answer is C .

This output is possible for a largest-first selection sort.

It cannot be a smallest-first selection sort because selection sort keeps the invariant that, after n iterations, the first n items in the list are fully-sorted. So, if this was a selection sort, you would expect that the element with the value 0 would have been sorted to index 0 in the first iteration. Thus, the list would look like 0 1 2 3 ... after the first 4 iterations.

The output is possible for both a largest-first and smallest-first insertion sort because insertion sort has a different invariant. In an insertion sort, after n iterations, the first n items in the list (last n for a largest-first sort) are sorted with respect to each other, but they're not necessarily in their final position in the list.

For a smallest-first insertion sort, after 4 iterations, it's possible to see the "out of place" 0 because the sort hasn't yet iterated enough to re-position the 0 in its correct index.

This could be the output of either a largest-first selection sort or an insertion sort. If the array originally looked like that, after 4 iterations of either algorithm, it would still look like that. It couldn't be a smallest-first selection sort, since after 4 iterations of that, the first 4 items of the array would be 0 1 2 3 .

According to me answer is C. *

Selection sort-
    for(int x=0; x<n; x++){
        int index_of_min = x;
        for(int y=x; y<n; y++){
            if(array[index_of_min]>array[y]){
                index_of_min = y;
            }
        }
        int temp = array[x];
        array[x] = array[index_of_min];
        array[index_of_min] = temp;
    }

*

It is so because in Selection sort you traverse array right of the x in array and find minimum element in every pass and replace that index with a[x]. In above case

1 2 3 4 5 0 6 7 8 9

min element is 0 and it should be the leftmost element if selection sort is used because selection sort in the first pass minimum in the whole array should be at a[0]. Insertion Sort is possible because in insertion sort we pick up element and keep on comparing the current element with the elements to its left till the current element is at its correct sorted location.What i am trying to say is: *

> Here is an example: for sorting the array the array 52314  First, 2 is
> inserted before 5, resulting in 25314 Then,  3 is inserted between 2
> and 5, resulting in 23514 Next, one is inserted at the start, 12354
> Finally, 4 is inserted between 3 and 5, 12345.

* So Insertion Sort is possible as it has finished sorting up to its fourth pass.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM