morry2341

selectionSort

Apr 19th, 2023
686
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.46 KB | None | 0 0
  1. public static void selectionSort(int[] f){
  2.   for(int i = f.length-1; i >= 0; --i){
  3.     int maxId = indexOfGreatestElement(f,i);
  4.     swap(f,maxId,i);
  5.   }
  6. }
  7.  
  8. public swap(int[] f, int x, int y){
  9.   int tmp = f[x];
  10.   f[x] = f[y];
  11.   f[y] = tmp;
  12. }
  13.  
  14. public static void indexOfGreatestElement(int[] f, int i){
  15.   int maxIdx = 0; //maxWerte initialisieren
  16.   for(int x = i; x> 0; --x){
  17.     if(f[maxIdx] < f[x]){
  18.       maxIdx = x;
  19.     }
  20.   }
  21.  
  22.   return maxIdx;
  23. }
  24.  
  25.  
Advertisement
Add Comment
Please, Sign In to add comment