Posts

Showing posts with the label Selection sort

Selection sort in C++

 #include<iostream> using namespace std ; int main () { int min , i , j , temp , a [ 100 ], n ; cout << "\tSelection sort" ; cout << "\nEnter size of array=\t" ; cin >> n ; cout << "\nEnter actual array element:\n" ; for ( i = 0 ; i < n ; i ++) cin >> a [ i ]; for ( i = 0 ; i < n ; i ++) { min = i ; for ( j = i + i ; j < n ; j ++) { if ( a [ min ]> a [ j ]) min = j ; } temp = a [ i ]; a [ i ]= a [ min ]; a [ min ]= temp ; } cout << "\nResultant array=\n" ; for ( i = 0 ; i < n ; i ++) cout << a [ i ]<< " " ; } Output:  Selection sort Enter size of array=    5 Enter actual array element: 9 7 8 4 6 Resultant array= 4 6 7 8 9

Selection sort in C

 #include<stdio.h> int main () { int n,i,arr [ 10 ] ,num,found = 0 ; printf ( "Enter size of array=\n" ); scanf ( "%d" ,&n ); printf ( "Enter array element\n" ); for ( i = 0 ; i < n ; i ++) { printf ( "\narr[%d]=" ,i ); scanf ( "%d", & arr [ i ]); } printf("which no want to find\n" ); scanf ( "%d" , & num ); for ( i = 0 ; i < n ; i ++) { if ( arr [ i ]== num ) { found = 1 ; printf ( "Enter element is found in the array" ); break ; } } if ( found == 0 ) printf( "Enter element is not found" ); } output (element not found) Enter size of array= 5 Enter array element arr[0]=3 arr[1]=5 arr[2]=4 arr[3]=7 arr[4]=3 which no want to find 8 Enter element is not found output: (entered element found) Enter size of array= 5 Enter array element arr[0]=5 arr[1]=6 arr[2]=7 arr[3]=3 arr[4]=4 which no want to find 4 Entered element i...