I have some questions about selection sort.I\'m a little bit confused.
int [] arr = {5,4,3,2,1}; // This is my array
int min = 0;
for(int i = 0;i&l
The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning. The algorithm maintains two subarrays in a given array.
1) The subarray which is already sorted. 2) Remaining subarray which is unsorted.
In every iteration of selection sort, the minimum element (considering ascending order) from the unsorted subarray is picked and moved to the sorted subarray.
Example:
arr[] = 64 25 12 22 11
// Find the minimum element in arr[0...4]
// and place it at beginning
11 25 12 22 64
// Find the minimum element in arr[1...4]
// and place it at beginning of arr[1...4]
11 12 25 22 64
// Find the minimum element in arr[2...4]
// and place it at beginning of arr[2...4]
11 12 22 25 64
// Find the minimum element in arr[3...4]
// and place it at beginning of arr[3...4]
11 12 22 25 64
Java Code is:
void sort(int arr[])
{
int n = arr.length;
// One by one move boundary of unsorted subarray
for (int i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
int min_idx = i;
for (int j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
// Swap the found minimum element with the first
// element
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
And Be Clear that Arrays are Passed by Reference!
pass the unsorted array get the sorted array
public int[] selectionSort(int[] list) {
int i, j, minValue, minIndex, temp = 0;
for (i = 1; i < list.length; i++) {
minValue = list[i];
minIndex = i;
j = i - 1;
for (j = i; j < list.length; j++) {
if (list[j] < minValue) {
minValue = list[j];
minIndex = j;
}
}
if (list[i] > minValue) {
temp = list[i];
list[i] = list[minIndex];
list[minIndex] = temp;
}
}
return list;
}
selection sort is about finding the min value in each step of loop. you didn't find out the min value (by if statement maybe), just simply exchange the value in your inner loop. so you actually didn't do a sort.
correction based on your implementation:
final int[] arr = { 5, 4, 3, 2, 1 }; // This is my array
int min;
for (int i = 0; i < arr.length; i++) {
// Assume first element is min
min = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[min]) {
min = j;
}
}
if (min != i) {
final int temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
System.out.println(arr[i]);// I print the in ascending order
}
this should give you output:
1
2
3
4
5
/* Implementation of selection sort */
import java.util.Arrays;
import java.util.Scanner;
public class SelectionSort {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of elements of the array");
int n = in.nextInt();
int []a = new int[n];
System.out.println("Enter the integer array of elements");
for (int i=0; i<n ; i++)
{
a[i] = in.nextInt();
}
System.out.println("Before Sorting: "+Arrays.toString(a));
a = sort(a);
System.out.println("After Sorting: "+Arrays.toString(a));
}
private static int[] sort(int[] a) {
// TODO Auto-generated method stub
for(int i=0; i<a.length-1;i++)
{
int index=i;
for(int j=i+1; j<a.length;j++)
if(a[j]<a[index])
{
index=j;
}
int small = a[index];
a[index] = a[i];
a[i]=small;
}
return a;
}
}
public class Selectionsort{
public static int arr[]; public static int y;
public static void main( String args[] ){
System.out.println("Enter number of element you want to enter for sorting");
int nofele= Integer.parseInt(args[0]);
System.out.println("Enter number of element entered for sorting is "+ "\n" +nofele);
arr = new int[nofele];
System.out.println("Entered array is");
for(int i=1,j=0;i<=nofele;i++,j++){
arr[j]=Integer.parseInt(args[i]);
System.out.println(arr[j]);
}
System.out.println("Sorted array for selection sort is ");
for(int k=0;k<nofele-1;k++) {
for(int l=nofele-k,b=1;l>=2;l--,b++){
if(arr[k]>arr[k+b]){
int temp=arr[k];
arr[k]=arr[k+b];
arr[k+b] = temp;
}
}
System.out.println(arr[k]);
}
System.out.println(arr[nofele-1]);
}
}
public class SelectionSort {
public static void main(String[] args) {
int[] A = {5,4,3,2,1};
int l = A.length;
for (int i = 0; i < l-1; ++i ){
int minPos = i;
// Find the location of the minimal element
for (int j = i + 1; j < l; ++j){
if ( A[j] < A[minPos]){
minPos = j;
}
}
if (minPos != i){
int temp = A[i];
A[i] = A[minPos];
A[minPos] = temp;
}
}
System.out.println(Arrays.toString(A));
}
}