selection sort in java

Solutions on MaxInterview for selection sort in java by the best coders in the world

showing results for - "selection sort in java"
Samantha
16 Jul 2017
1public static void SelectionSort(int[] arr)
2{
3  int small;
4  for (int i = 0; i <arr.length - 1; i++)
5  {
6    small = i;
7    for (int j = i + 1; j < arr.length; j++)
8    {
9      //if current position is less than previous smallest
10      if (arr[j] < arr[small])
11      {
12        small = j;
13        
14        //swap values
15        int temp = arr[i];
16        arr[i] = arr[small];
17        arr[small] = temp; 
18      }
19  	}
20  }
21}
Liah
20 Nov 2017
1// example on selection sort java
2public class SelectionSortInJava
3{
4   void toSort(int[] arrNum)
5   {
6      int number = arrNum.length;
7      for(int a = 0; a < number - 1; a++)
8      {
9         // finding minimum element
10         int minimum = a;
11         for(int b = a + 1; b < number; b++)
12         {
13            if(arrNum[b] < arrNum[minimum])
14            {
15               minimum = b;
16            }
17         }
18         // swapping minimum element with first element
19         int temp = arrNum[minimum];
20         arrNum[minimum] = arrNum[a];
21         arrNum[a] = temp;
22      }
23   }
24   // printing array
25   void displayArray(int[] arrPrint)
26   {
27      int num = arrPrint.length;
28      for(int a = 0; a < num; ++a)
29      {
30         System.out.print(arrPrint[a] + " ");
31      }
32      System.out.println();
33   }
34   public static void main(String[] args)
35   {
36      SelectionSortInJava obj = new SelectionSortInJava();
37      int[] arrInput = {5, 4, -3, 2, -1};
38      obj.toSort(arrInput);
39      System.out.println("After sorting : ");
40      obj.displayArray(arrInput);
41   }
42}
Alina
29 May 2017
1static void selectionSort(int[] arr) {
2        int lowest, lowestIndex;
3        for(int i = 0; i < arr.length -1; i++) {
4            //Find the lowest
5            lowest = arr[i];
6            lowestIndex = i;
7            for(int j = i; j < arr.length; j++) {
8                if(arr[j] < lowest) {
9                    lowest = arr[j];
10                    lowestIndex = j;
11                }
12            }
13            //Swap
14            if(i != lowestIndex) {
15                int temp = arr[i];
16                arr[i] = arr[lowestIndex];
17                arr[lowestIndex] = temp;
18            }
19            
20        }
21    }
queries leading to this page
selection sort algorithm c 2b 2b codewhat is selection sortlinear sorting in javawhat is selection sorting in array javaselect sort javajava program of selection sort using arrayselection sort javselection sort in jkavajava selection sortsort selection javaselection sort descending order javaselection sort c 2b 2bselection sort javaselection sort java programselection sort method javaselecton sort code ofr javaselection sort auf liste javaselection sort of list javaselection sort passes formula javaselection sort code javaselection sorting algorithm in javasuppose you have implemented the selectionsortarray class add a method called median 28 29 to the selectionsortarray class in the selectionsortarray java program this method should return the median value in the arrayimplement selection sort and print the index which gets swapped at each step selection sort logic in javajava array selection sort functionselection sort list javajava selection sort stringjava code for selection sortselection sort list explanation in javaselection sorting algorithmsgiven a set of 4 integers 2c sort using selection sortselection sort javain selection sort print the index which gets swapped at each stepselection sort ajvaselectionsort in javaselection sort java implementationjava selection sort with main methodin place selection sort javawhat is selection sort in javasorting arrays in c 2b 2bselection sort in java programmethod selection in array javafrom selection sort import selectionwhat is the list after completing the first outer loop of selection sort 3fjava selection sort iastateinsertion sort geeksforgeeksjava selection sort codec 2b 2b selection sorthow to make a selection sort in javahow does selection sort work in java with examplselection sort program in cwhat is a selection sort in javaselection sort java metodoselection sort algoselection sort execution javaselection sort algorithm in javaselection sort coding in javaselection sort program in javaimplement selection sort in javasort selectionselectin sort in javajava sort selectionselection sort implementation javaselection sort in ascending order javaselection sort jjavaselection sort java codethje code of selection sort in javasort a set in javaselection sort java simpleselection sort java descedng orderhow to selection sort javaselection sort in java programizsearch sort programme javaslesction sort javaselection sort program in oops javaselection sort code in javaselection sort first swapselection sort java explanationjava section sortsimple selection sort program in javaselection sort in descending order in java geeks for geeksjava array selection sortselection sort explanation in javaselection sort java return sorted arrayselection sort java explainedseletion sort algorithmcode for selection sortselection sort java algorithmselection sort with list javajava selection sortselection sort codeslection sort in javaselection sort array in javabest java programs selection sortselection sort in array in javaselection sort method in javathe selection sort javaascending order using selection sort in javahow to use selection sort in javajava selection sortselection sorting javaselection sort ascending order javaselection sort functionselectionsort javaselection algorithm javajava selection sort programselection sort jaainplace selection sort an integer arraywhat is selection sort 3f4 write a program to sort the given array using selection sort selection sort algorithm with example in java15 29 write a program to implement selection sort using array as a data structure selection sort java int array algorithm for selection sort in javasuppose you have implemented the selectionsortarray class add a method called median 28 29 to the selection sort array class in the selectionsortarray java program this method should return the median value in the arrayselecting sort javaselection sort in c 2b 2bsort array using selection sortselection sorytsorting java selection sortselection sortsort array with selection sort javaselection sort in cselection sort syntaxselection sort in java with given arrayvoid selectionsortselection sort algorithm javaselection sort in javaselection sort algorithm java explainedhow does selection sort work in java with exampleselection sort in java given arrayhow to perform a selection sort javaselection sorting in javaselection sort algorithm code javaselection sort array javavoting system using selection sortselection sort arraysselection sort java exampleselection sort javaselection sort java programming 1selection sort java what isselection sort algorithm java examplesorting data by using selection sort javahow to make a method that sorts an array of integers in ascending order by the selection sort methodprint selection sort swaped indexint selectionsort 5cselectkpn sort javawrite a program to sort an array using selection sort with time complexity java selection sort methodselection sort in java