1#include<stdio.h>
2int main(){
3 /* Here i & j for loop counters, temp for swapping,
4 * count for total number of elements, number[] to
5 * store the input numbers in array. You can increase
6 * or decrease the size of number array as per requirement
7 */
8 int i, j, count, temp, number[25];
9
10 printf("How many numbers u are going to enter?: ");
11 scanf("%d",&count);
12
13 printf("Enter %d elements: ", count);
14 // Loop to get the elements stored in array
15 for(i=0;i<count;i++)
16 scanf("%d",&number[i]);
17
18 // Logic of selection sort algorithm
19 for(i=0;i<count;i++){
20 for(j=i+1;j<count;j++){
21 if(number[i]>number[j]){
22 temp=number[i];
23 number[i]=number[j];
24 number[j]=temp;
25 }
26 }
27 }
28
29 printf("Sorted elements: ");
30 for(i=0;i<count;i++)
31 printf(" %d",number[i]);
32
33 return 0;
34}
1public static void SelectionSort(int[] arr)
2{
3 int small;
4 for (int i = 0; i <arr.length - 1; i++)
5 {
6 small = i;
7 for (int j = i + 1; j < arr.length; j++)
8 {
9 //if current position is less than previous smallest
10 if (arr[j] < arr[small])
11 {
12 small = j;
13
14 //swap values
15 int temp = arr[i];
16 arr[i] = arr[small];
17 arr[small] = temp;
18 }
19 }
20 }
21}
1def ssort(lst):
2 for i in range(len(lst)):
3 for j in range(i+1,len(lst)):
4 if lst[i]>lst[j]:lst[j],lst[i]=lst[i],lst[j]
5 return lst
6if __name__=='__main__':
7 lst=[int(i) for i in input('Enter the Numbers: ').split()]
8 print(ssort(lst))
1 1 // Fig. 8.15: fig08_15.cpp
2 2 // This program puts values into an array, sorts the values into
3 3 // ascending order and prints the resulting array.
4 4 #include <iostream>
5 5 using std::cout;
6 6 using std::endl;
7 7
8 8 #include <iomanip>
9 9 using std::setw;
1010
1111 void selectionSort( int * const, const int ); // prototype
1212 void swap( int * const, int * const ); // prototype
1313
1414 int main()
1515 {
1616 const int arraySize = 10;
1717 int a[ arraySize ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };
1818
1919 cout << "Data items in original order\n";
2020
2121 for ( int i = 0; i < arraySize; i++ )
2222 cout << setw( 4 ) << a[ i ];
2323
2424 selectionSort( a, arraySize ); // sort the array
2525
2626 cout << "\nData items in ascending order\n";
2727
2828 for ( int j = 0; j < arraySize; j++ )
2929 cout << setw( 4 ) << a[ j ];
3030
3131 cout << endl;
3232 return 0; // indicates successful termination
3333 } // end main
3434
3535 // function to sort an array
3636 void selectionSort( int * const array, const int size )
3737 {
3838 int smallest; // index of smallest element
3939
4040 // loop over size - 1 elements
4141 for ( int i = 0; i < size - 1; i++ )
4242 {
4343 smallest = i; // first index of remaining array
4444
4545 // loop to find index of smallest element
4646 for ( int index = i + 1; index < size; index++ )
4747
4848 if ( array[ index ] < array[ smallest ] )
4949 smallest = index;
5050
5151 swap( &array[ i ], &array[ smallest ] );
5252 } // end if
5353 } // end function selectionSort
5454
5555 // swap values at memory locations to which
5656 // element1Ptr and element2Ptr point
5757 void swap( int * const element1Ptr, int * const element2Ptr )
5858 {
5959 int hold = *element1Ptr;
6060 *element1Ptr = *element2Ptr;
6161 *element2Ptr = hold;
6262 } // end function swap
63