Advertisement
uopspop

Untitled

May 5th, 2016
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void swap(int * const elementPtr1, int * const elementPtr2 ){
  5.     *elementPtr1 = *elementPtr1 ^ *elementPtr2;
  6.     *elementPtr2 = *elementPtr2 ^ *elementPtr1;
  7.     *elementPtr1 = *elementPtr2 ^ *elementPtr1;
  8. }
  9.  
  10.  
  11. void selectionSort(int * const array, const int size){
  12.  
  13.     int smallest;  
  14.     for (int i = 0; i < size - 1; i++){
  15.         smallest = i;
  16.         for (int j = i + 1; j < size; j++){
  17.             if (array[j] < array[smallest]){
  18.                 smallest = j;
  19.             }
  20.         }  
  21.         if (smallest != i){
  22.             swap(&array[i],&array[smallest]);
  23.         }
  24.     }
  25. }
  26.  
  27.  
  28.  
  29.  
  30. int main(){
  31.    
  32.     const int arraySize = 10;
  33.     int a[arraySize] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37};  
  34.     for (int i = 0; i < arraySize; i++){
  35.         cout << a[i] << " ";
  36.     }
  37.     cout << endl;
  38.  
  39.     selectionSort(a,arraySize);
  40.    
  41.     for (int i = 0; i < arraySize; i++){
  42.         cout << a[i] << " ";
  43.     }
  44.     cout << endl;
  45.    
  46.    
  47.    
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement