Created
November 29, 2015 22:15
-
-
Save stevyhacker/b7e0e016cf2ffb756349 to your computer and use it in GitHub Desktop.
Selection sort in java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class NizoviCas2SelectionSort { | |
public static void stampajNiz(int[] a) { | |
for (int i = 0; i < a.length; i++) { | |
System.out.print(a[i] + " "); | |
} | |
} | |
// vraća indeks najmanjeg elementa u dijelu niza a [ind...n | |
public static int indeksNajmanjeg(int[] a, int indeks) { | |
int trenutnoNajmanji = a[indeks]; | |
int indeksTrazeni = indeks; | |
for (int i = indeks + 1; i < a.length; i++) { | |
if (a[i] < trenutnoNajmanji) { | |
trenutnoNajmanji = a[i]; | |
indeksTrazeni = i; | |
} | |
} | |
return indeksTrazeni; | |
} | |
public static void selectionSort(int[] a) { | |
for (int i = 0; i < a.length - 1; i++) { | |
int n = indeksNajmanjeg(a, i); | |
if (i != n) { | |
int t = a[i]; | |
a[i] = a[n]; | |
a[n] = t; | |
} | |
} | |
} | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
int[] a = { 8, 9, 3, 7, 1, 5 }; | |
stampajNiz(a); | |
selectionSort(a); | |
System.out.println(); | |
stampajNiz(a); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment