|
1 #include < iostream > 2 using std::cout; 3 using std::endl; 4 #include < iomanip > 5 using std::setw; 6 7 void selectionSort( int * const , const int ); 8 void swap( int * const , int * const ); 9 10 int main() 11 { 12 const int arraySize = 10 ; 13 int a[arraySize] = { 2 , 6 , 4 , 8 , 10 , 12 , 89 , 68 , 45 , 37 } ; 14 15 cout << " Data Item in original order\n " ; 16 17 for ( int i = 0 ;i < arraySize;i ++ ) 18 { 19 cout << setw( 4 ) << a[i]; 20 } 21 selectionSort(a,arraySize); 22 23 cout << " Data Item in ascending order\n " ; 24 25 for ( int j = 0 ;j < arraySize;j ++ ) 26 { 27 cout << setw( 4 ) << a[j]; 28 } 29 30 cout << endl; 31 32 return 0 ; 33 } 34 void selectionSort( int * const array, const int size) 35 { 36 int smallest; 37 38 for ( int i = 0 ;i < size;i ++ ) 39 { 40 smallest = i; 41 42 for ( int index = i + 1 ;index < size;index ++ ) 43 { 44 if (array[smallest] > array[index]) 45 { 46 // smallest=index; 47 48 swap( & array[smallest], & array[index]); 49 } 50 } 51 } 52 // for(int i=0;i<size;i++) 53 // { 54 // smallest=i; 55 // 56 // for (int index=i+1;index<size;index++) 57 // { 58 // if(array[smallest]>array[index]) 59 // smallest=index; 60 // } 61 // if(smallest!=i) 62 // swap(&array[smallest],&array[i]); 63 // 64 // } 65 66 } 67 void swap( int * const elemet1, int * const element2) 68 { 69 int i =* elemet1; 70 * elemet1 =* element2; 71 * element2 = i; 72 } 73 74
|