开发者

What is the name of this sorting algorithm?

开发者 https://www.devze.com 2023-02-18 04:39 出处:网络
for(int i=0; i<n-1; i++) { for(int j=i+1; j<n; j++) { if(a[i] > a[j]) { /* Swap a[i] and a[j] */ } }
for(int i=0; i<n-1; i++)
{
    for(int j=i+1; j<n; j++)
    {
        if(a[i] > a[j])
        {
            /* Swap a[i] and a[j] */
        }
    }
}

P.S. Given the name of an algorithm, one can easily find relevent source code. But I find it hard to do the vice-versa :D

Edit Oh! If that is bubble sort, then what is the name of this:

for(int i=0; i<n; i++)
{
    for(int j=0; j<n-1; j++)
    {
        if(a[j] > a[j+1])
        {
            /* Swap a[j] and a[j+1] */
        }
    }
}

I thought this second one "bubbles" the smaller elements up, so I thought this was actually bubble sort. If the first one is bubble so开发者_Python百科rt, what's the name of the second one?


First is the Selection Sort and the second one you added is Bubble sort!


First one is selection sort, second one is Bubble sort


The name of this algorithm is bubble sort.

edit: sorry for mistake (confused i with j in a[i] > a[j]). The first one is selection sort


There are so many sorting algorithms. Here are just a few:

  • Selection sort
  • Bubble sort
  • Heap Sort
  • Merge Sort
  • Counting Sort
  • Comb Sort
  • Quick Sort
0

精彩评论

暂无评论...
验证码 换一张
取 消