Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Write a description of class SelectionSort here.
- *
- * @author (your name)
- * @version (a version number or a date)
- */
- class SelectionSort
- {
- void selectionSort(int arr[])
- {
- int len = arr.length;
- for(int i=0; i<len-1; i++)
- {
- //finding the minimum element in the unsorted part of array
- int min = i;
- for(int j=i+1; j<len; j++)
- if(arr[j] < arr[min])
- min = j;
- //swapping the found minimum element with the first
- //element of the sorted sub array using temp variable
- int temp = arr[min];
- arr[min] = arr[i];
- arr[i] = temp;
- }
- }
- //display the array element
- void printArr(int arr[])
- {
- for(int i=0; i<arr.length; i++)
- System.out.print(arr[i] + " ");
- System.out.println();
- }
- public static void main(String[] args)
- {
- SelectionSort obj = new SelectionSort();
- int numarr[] = {101,5,18,11,80,67};
- System.out.print("Before Selection Sort: ");
- obj.printArr(numarr);
- //calling method for selection sorting
- obj.selectionSort(numarr);
- System.out.print("After Selection Sort: ");
- obj.printArr(numarr);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment