GeeksforGeeks » Interview Questions

Sorting Question

(3 posts)
  1. udp
    guest
    Posted 6 months ago #

    if we have set of n elements then

    1) which sorting method uses least number of comparisons??

    2) which sorting method uses least number of swaps??

    3) suppose the array is 2 8 4 6 5 9
    if we want to swap 8 and 5 the cost is 2(5-2)=6 .here 5 and 2 are indices of 5 and 8.
    so what sorting method minimizes the cost, i want the answer in general case ,not for this particular array. it is also called least distance sorting

  2. kartik
    Moderator
    Posted 6 months ago #

    2) which sorting method uses least number of swaps??

    Selection sort makes minimum number of swaps among the well known sorting algos. There is one more sorting algo "Cycle Sort" that makes less number of swaps than the selection sort. See http://www.geeksforgeeks.org/archives/10397

  3. kartik
    Moderator
    Posted 6 months ago #

    1) which sorting method uses least number of comparisons??

    There are two kind of sorting algos:
    a) Comparison Based: Bubble Sort, Quick Sort, Merge Sort
    b) Not comparison based: Counting Sort, Bucket Sort

    So there are algos that don't do even a single comparison.

    Among the comparison based algos, it depends on the input type. Some sorting algo like QuickSort do more comparisons for already sorted data, other sorting algos like insertion sort do less comparisons for sorted data.


Reply

You must log in to post.

RSS feed for this topic