//*****************************************************************// We simply introduce Selection Sort//*****************************************************************importjava.util.Random;publicclass SelectionSort{publicstaticvoid main(String[] HAHA){//Create an object of random classRandom generator =newRandom();//Creating an integer array of size 10int list []=newint[10];//Putting random numbers into the array we've createdfor(int i=0;i<10;i++)
list[i]= generator.nextInt(1000);//We put 1000//because we want integers range from 0 to 1000for(int i=0;i<list.length-1;i++){int location = i;//To specify the location of the loopint max = list[i];//We specify the maximum number//We choose the arrays ith element since it can be the//maximum number.for(int j=i;j<list.length;j++){//We compare max and list[j] if max is less we make//it the list[j] because we want maximum elementif(list[j]>max){
max = list[j];
location = j;}}//We create a temprorary integer in order not to lose the//data in list[i] then we change the data between them.int temp = list[i];
list[i]= list[location];
list[location]= temp;}//We simply list the arrayfor(int i=0;i<list.length;i++)System.out.println(list[i]+"\t");}}
Henüz yorum yok.