What is the difference between the following two sorting algorithms?

Oct 20, 2014 at 8:08am
I think both of this are algorithm of selection sort.

but what is the difference between this two algorithms

Algo 1:

1
2
3
4
5
6
7
8
9
10
11
12
13
void selectionSort(int arr[], int n)
{
    for(int i=0; i<7; i++)
    {
        for(int j=i+1; j<7; j++)
        {
            if(a[j]<a[i])
            {
                swap(a[i],a[j]);
            }
        }
    }
}



Algo 2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void selectionSort(int arr[], int n)
{
    int i, j, min_idx;

    for (i = 0; i < n-1; i++)
    {
        min_idx = i;
        for (j = i+1; j < n; j++)
          if (arr[j] < arr[min_idx])
            min_idx = j;

        swap(arr[min_idx], arr[i]);
    }
}
Oct 20, 2014 at 8:14am
not much... but the top one can potentially swap more than one pair per iteration of i. whereas the bottom one basically finds the lowest candidate in the list before making the swap.
Oct 20, 2014 at 10:34am
first one is bubble sort
Topic archived. No new replies allowed.