import java.io.*;
// Selection sort search for the smallest number in the array
// saves it in the minimum spot and then repeats searching
// through the entire array each time
class SelectionSortTest {
public static void main (String[] args) {
int[] a={5,2,9,1,23};
// ascending order
for(int i=0;i<a.length;i++){
int minimum=i;
for(int j=minimum;j<a.length;j++){
// To change direction of sort just change
// this from > to <
if(a[minimum]<a[j]){
minimum=j;
}
}
int temp=a[i];
a[i]=a[minimum];
a[minimum]=temp;
}
for( int i=0;i<a.length;i++){
System.out.print(" "+a[i]);
}
System.out.println(" ");
// descending order
for(int i=0;i<a.length;i++){
int minimum=i;
for(int j=minimum;j<a.length;j++){
// To change direction of sort just change
// this from > to <
if(a[minimum]> a[j]){
minimum=j;
}
}
int temp=a[i];
a[i]=a[minimum];
a[minimum]=temp;
}
for( int i=0;i <a.length;i++){
System.out.print(" "+a[i]);
}
}
}
// Selection sort search for the smallest number in the array
// saves it in the minimum spot and then repeats searching
// through the entire array each time
class SelectionSortTest {
public static void main (String[] args) {
int[] a={5,2,9,1,23};
// ascending order
for(int i=0;i<a.length;i++){
int minimum=i;
for(int j=minimum;j<a.length;j++){
// To change direction of sort just change
// this from > to <
if(a[minimum]<a[j]){
minimum=j;
}
}
int temp=a[i];
a[i]=a[minimum];
a[minimum]=temp;
}
for( int i=0;i<a.length;i++){
System.out.print(" "+a[i]);
}
System.out.println(" ");
// descending order
for(int i=0;i<a.length;i++){
int minimum=i;
for(int j=minimum;j<a.length;j++){
// To change direction of sort just change
// this from > to <
if(a[minimum]> a[j]){
minimum=j;
}
}
int temp=a[i];
a[i]=a[minimum];
a[minimum]=temp;
}
for( int i=0;i <a.length;i++){
System.out.print(" "+a[i]);
}
}
}
Comments
Post a Comment