Advertisement
uopspop

Untitled

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