(2 intermediate revisions by the same user not shown)
Line 3: Line 3:
 
==Recursive solution==
 
==Recursive solution==
 
<syntaxhighlight lang="c" name="selectionsort_rec">
 
<syntaxhighlight lang="c" name="selectionsort_rec">
 +
#include <stdio.h>
  
 
void selection_sort(int *a, int n)
 
void selection_sort(int *a, int n)
Line 37: Line 38:
  
 
</syntaxhighlight>
 
</syntaxhighlight>
<!--
+
 
 
==Non-Recursive solution==
 
==Non-Recursive solution==
 
<syntaxhighlight lang="c" name="selectionsort_rec">
 
<syntaxhighlight lang="c" name="selectionsort_rec">
 +
 +
#include <stdio.h>
  
 
void selection_sort(int *a, int n)
 
void selection_sort(int *a, int n)
 
{
 
{
        if(n == 1)
+
         int big, i, j;
                return;
 
         int big = 0, i;
 
 
         for(i = 0; i < n; i++)
 
         for(i = 0; i < n; i++)
   
+
        {
        for(i = 1; i < n; i++)
+
                big = 0;
                if(a[i] > a[big])
+
                for(j = 1; j < n-i; j++)
                        big = i;
+
                        if(a[j] > a[big])
 
+
                                big = j;
        /*swap a[n-1] and a[big]*/
+
 
        int temp = a[n-1];
+
                /*swap a[n-1] and a[big]*/
        a[n-1] = a[big];
+
                int temp = a[n-i-1];
        a[big] = temp;
+
                a[n-i-1] = a[big];
         selection_sort(a, n-1);
+
                a[big] = temp;
 +
         }
 
}
 
}
  
Line 69: Line 71:
 
                 scanf("%d", &a[i]);
 
                 scanf("%d", &a[i]);
 
         selection_sort(a, n);
 
         selection_sort(a, n);
         prntf("Printing sorted:\n");
+
         printf("Printing sorted:\n");
 
         for(i = 0; i < n; i++)
 
         for(i = 0; i < n; i++)
 
                 printf("%d ", a[i]);
 
                 printf("%d ", a[i]);
 
         return 0;
 
         return 0;
 
}
 
}
 +
  
 
</syntaxhighlight>
 
</syntaxhighlight>
-->
+
 
 
{{Template:FBD}}
 
{{Template:FBD}}
  
 
[[Category: Arrays]]
 
[[Category: Arrays]]
 +
[[Category: Sorting]]

Latest revision as of 11:24, 22 July 2014

Complexity and no. of swaps required

Recursive solution

<syntaxhighlight lang="c" name="selectionsort_rec">

  1. include <stdio.h>

void selection_sort(int *a, int n) {

       if(n == 1)
               return;
       int big = 0, i;
       for(i = 1; i < n; i++)
               if(a[i] > a[big])
                       big = i;
  
       /*swap a[n-1] and a[big]*/
       int temp = a[n-1];
       a[n-1] = a[big];
       a[big] = temp;
       selection_sort(a, n-1);

}


int main() {

       int a[100], i, n;
       printf("Enter n:");
       scanf("%d", &n);
       printf("Enter numbers: ");
       for(i = 0; i < n; i++)
               scanf("%d", &a[i]);
       selection_sort(a, n);
       printf("Printing sorted:\n");
       for(i = 0; i < n; i++)
               printf("%d ", a[i]);
       return 0;

}

</syntaxhighlight>

Non-Recursive solution

<syntaxhighlight lang="c" name="selectionsort_rec">

  1. include <stdio.h>

void selection_sort(int *a, int n) {

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

}


int main() {

       int a[100], i, n;
       printf("Enter n:");
       scanf("%d", &n);
       printf("Enter numbers: ");
       for(i = 0; i < n; i++)
               scanf("%d", &a[i]);
       selection_sort(a, n);
       printf("Printing sorted:\n");
       for(i = 0; i < n; i++)
               printf("%d ", a[i]);
       return 0;

}


</syntaxhighlight>




blog comments powered by Disqus

Complexity and no. of swaps required

Recursive solution[edit]

<syntaxhighlight lang="c" name="selectionsort_rec">

void selection_sort(int *a, int n) {

       if(n == 1)
               return;
       int big = 0, i;
       for(i = 1; i < n; i++)
               if(a[i] > a[big])
                       big = i;
  
       /*swap a[n-1] and a[big]*/
       int temp = a[n-1];
       a[n-1] = a[big];
       a[big] = temp;
       selection_sort(a, n-1);

}


int main() {

       int a[100], i, n;
       printf("Enter n:");
       scanf("%d", &n);
       printf("Enter numbers: ");
       for(i = 0; i < n; i++)
               scanf("%d", &a[i]);
       selection_sort(a, n);
       printf("Printing sorted:\n");
       for(i = 0; i < n; i++)
               printf("%d ", a[i]);
       return 0;

}

</syntaxhighlight>



blog comments powered by Disqus