the code was very helpful but I am having a problem.I have to use Selection sort for a table with 1000 elements and they are listed from 1-1000 so are the indexes.I can display elements before being listed(even though the are already listed) but it seems that the selection sort isn't working cuz after being listed the console displays 111111111.....this is the code:public class SelectionSort {
public static void main(String[] args) {
long startTime =System.nanoTime();
int i;
int []A =new int[1000];
int Total=0;
for( i = 1; i < A.length; i++)
{A[0]=0;
A[i]=A[0]+1;
Total =Total+ A[i];
System.out.print("Vlerat para renditjes ");
System.out.print( Total+" ");
System.out.println();}
for( i = 1; i < A.length; i++)
selection_sort(A, A.length);
System.out.print("Vlerat pas renditjes :\n");
for(i= 1; i<A.length; i++)
System.out.print(A[i]+" ");
System.out.println();
long stopTime = System.nanoTime();
long elapsedTime=stopTime-startTime;
System.out.println("Koha e ekzekutimit:"+elapsedTime+" nanosekonda");
}
public static void selection_sort(int A[], int n){
for (int i=1;i<=A.length-1;i++)
{int min=i;
for(int j=i+1;j<=A.length;j++){
if(A[j]<A[min]){
min=j;
}
}
int temp = A[i];
A[i] = A[min];
A[min] = temp;
}
}
}
selection sort techniquearti nigam April 18, 2011 at 12:22 AM
i want the of selection sort in java
downloadJorge July 1, 2011 at 4:21 PM
i want insertion sort...!
computerrajat sahay February 12, 2013 at 1:24 PM
my fovourate subject computer
Helppp!!!!Samantha January 13, 2013 at 7:10 PM
the code was very helpful but I am having a problem.I have to use Selection sort for a table with 1000 elements and they are listed from 1-1000 so are the indexes.I can display elements before being listed(even though the are already listed) but it seems that the selection sort isn't working cuz after being listed the console displays 111111111.....this is the code:public class SelectionSort { public static void main(String[] args) { long startTime =System.nanoTime(); int i; int []A =new int[1000]; int Total=0; for( i = 1; i < A.length; i++) {A[0]=0; A[i]=A[0]+1; Total =Total+ A[i]; System.out.print("Vlerat para renditjes "); System.out.print( Total+" "); System.out.println();} for( i = 1; i < A.length; i++) selection_sort(A, A.length); System.out.print("Vlerat pas renditjes :\n"); for(i= 1; i<A.length; i++) System.out.print(A[i]+" "); System.out.println(); long stopTime = System.nanoTime(); long elapsedTime=stopTime-startTime; System.out.println("Koha e ekzekutimit:"+elapsedTime+" nanosekonda"); } public static void selection_sort(int A[], int n){ for (int i=1;i<=A.length-1;i++) {int min=i; for(int j=i+1;j<=A.length;j++){ if(A[j]<A[min]){ min=j; } } int temp = A[i]; A[i] = A[min]; A[min] = temp; } } }
Inefficient ProgramMr. Ianine February 24, 2012 at 11:44 PM
Scrub, I can do this in one line
selection sortali November 2, 2012 at 12:25 AM
im confused in selection and insertion sort...it seems same to me ....help plz
CorrectionArmyHill01 April 26, 2013 at 8:03 AM
The inequality in your if statement is reversed. Switch it and all is perfect. Thanks for the working algorithm!
Post your Comment