Code Scrappers


Selection Sort - C program


                    #include<stdio.h>
                    #include<stdlib.h>

                    void main()
                    {
                        int arr[10];

                        int n, minimum, i, j, temp;

                        printf("enter the size of array \n");

                        scanf("%d", &n);

                        printf("enter array elements\n");

                        for(i=0;i<n;i++)
                        {
                            scanf("%d", &arr[i]);
                        }

                        for(i=0;i<n;i++)
                        {
                            minimum = i;

                           for(j=i+1;j<n;j++)
                           {
                               if(arr[j] < arr[minimum])
                               {
                                    minimum = j;
                               }
                           }

                            temp = arr[i];

                            arr[i] = arr[minimum];

                            arr[minimum] = temp;

                        }

                        printf("sorted array\n");

                        for(i=0;i<n;i++)
                        {
                            printf("%d\n", arr[i]);

                        }

                    }

                



Output

            enter the size of array
            5
            enter array elements
            1
            4
            2
            8
            7
            sorted array
            1
            2
            4
            7
            8