Kuidas valiku sortimine töötab?
Selection Sort rakendab lihtsat sortimisalgoritmi järgmiselt:
- Algoritm otsib korduvalt madalaimat elementi.
- Vahetage praegune element madalaima väärtusega elemendiga
- Iga valiku sorteerimise / korduse korral vahetatakse elemendid.
Java-programm valiku sortimise rakendamiseks
pakett com.guru99;avalik klass SelectionSortAlgo {avalik staatiline void main (string a []){int [] myArray = {860,8,200,9};System.out.println ("------ Enne valiku sortimist -----");printArray (myArray);valik (myArray); // massiivi sortimine valiku sortimise abilSystem.out.println ("----- pärast valiku sortimist -----");printArray (myArray);}avalik staatiline tühimiku valik (int [] massiiv){jaoks (int i = 0; iVäljund:
------Before Selection Sort-----860 8 200 9Sort Pass Number 1Comparing 860 and 8860 is greater than 8Comparing 8 and 200Comparing 8 and 9Swapping Elements: New Array After Swap8 860 200 9Sort Pass Number 2Comparing 860 and 200860 is greater than 200Comparing 200 and 9200 is greater than 9Swapping Elements: New Array After Swap8 9 200 860Sort Pass Number 3Comparing 200 and 860Swapping Elements: New Array After Swap8 9 200 860-----After Selection Sort-----8 9 200 860