100% found this document useful (1 vote)
97 views4 pages

Selection Sort Sda

The selection sort algorithm works by iterating through an array, finding the minimum element, and swapping it into the front of the unsorted section. This has a time complexity of O(n^2) as it must iterate through the entire array n times, with an inner loop also iterating through the array each time to find the minimum. The algorithm iterates through the array, finds the index of the minimum element, swaps it into place, and repeats until the array is fully sorted.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
97 views4 pages

Selection Sort Sda

The selection sort algorithm works by iterating through an array, finding the minimum element, and swapping it into the front of the unsorted section. This has a time complexity of O(n^2) as it must iterate through the entire array n times, with an inner loop also iterating through the array each time to find the minimum. The algorithm iterates through the array, finds the index of the minimum element, swaps it into place, and repeats until the array is fully sorted.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

package com.java2novice.

algos;

public class MySelectionSort {

public static int[] doSelectionSort(int[] arr){

for (int i = 0; i < arr.length - 1; i++)


{
int index = i;
for (int j = i + 1; j < arr.length; j++)
if (arr[j] < arr[index])
index = j;

int smallerNumber = arr[index];


arr[index] = arr[i];
arr[i] = smallerNumber;
}
return arr;
}

public static void main(String a[]){

int[] arr1 = {10,34,2,56,7,67,88,42};


int[] arr2 = doSelectionSort(arr1);
for(int i:arr2){
System.out.print(i);
System.out.print(", ");
}
}
}
- See more at: http://www.java2novice.com/java-sorting-algorithms/selection-
sort/#sthash.eHHkGTK4.dpuf

The selection sort is a combination of searching and sorting. During each pass, the
unsorted element with the smallest (or largest) value is moved to its proper position in
the array. The number of times the sort passes through the array is one less than the
number of items in the array. In the selection sort, the inner loop finds the next
smallest (or largest) value and the outer loop places that value into its proper location.
Selection sort is not difficult to analyze compared to other sorting algorithms since
none of the loops depend on the data in the array. Selecting the lowest element
requires scanning all n elements (this takesn 1 comparisons) and then swapping it
into the first position. Finding the next lowest element requires scanning the
remaining n 1 elements and so on, for (n 1) + (n 2) + ... + 2 + 1 = n(n 1) / 2
(n2) comparisons. Each of these scans requires one swap for n 1 elements.

1 package com.java2novice.algos;

3 public class MySelectionSort {

4
public static int[] doSelectionSort(int[] arr){
5

6
for (int i = 0; i < arr.length - 1; i++)
7
{
8
int index = i;
9
for (int j = i + 1; j < arr.length; j++)
10 if (arr[j] < arr[index])
11 index = j;
12

13 int smallerNumber = arr[index];

14 arr[index] = arr[i];

15 arr[i] = smallerNumber;

}
16
return arr;
17
}
18

19
public static void main(String a[]){
20

21
int[] arr1 = {10,34,2,56,7,67,88,42};
22 int[] arr2 = doSelectionSort(arr1);
23 for(int i:arr2){
24 System.out.print(i);
25 System.out.print(", ");

26 }

}
27
}
28

29

30

Output:

2, 7, 10, 34, 42, 56, 67, 88,

- See more at: http://www.java2novice.com/java-sorting-algorithms/selection-


sort/#sthash.eHHkGTK4.dpuf

You might also like