Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Sorter {
- /*public static void selectionSort(int[] arr) {
- for (int n=arr.length; n > 1; n--) {
- int maxIndex = 0;
- for (int i=1; i < n; i++) {
- if (arr[i] > arr[maxIndex]) {
- maxIndex = i;
- }
- }
- // switch with last
- int temp = arr[maxIndex];
- arr[maxIndex] = arr[n-1];
- arr[n-1] = temp;
- }
- } */
- public static void selectionSort(String[] arr) {
- for (int n=arr.length; n > 1; n--) {
- int maxIndex = 0;
- for (int i=1; i < n; i++) {
- if (arr[i].compareTo(arr[maxIndex]) > 0) {
- maxIndex = i;
- }
- }
- // switch with last
- String temp = arr[maxIndex];
- arr[maxIndex] = arr[n-1];
- arr[n-1] = temp;
- }
- }
- public static void printArray(int[] list) {
- System.out.print("[");
- System.out.print(list[0]);
- for (int i=1; i < list.length; i++) {
- System.out.print("," + list[i]);
- }
- System.out.println("]");
- }
- public static void printArray(String[] list) {
- System.out.print("[");
- System.out.print(list[0]);
- for (int i=1; i < list.length; i++) {
- System.out.print("," + list[i]);
- }
- System.out.println("]");
- }
- public static void main(String[] args) {
- System.out.println("Enter list of integers to be sorted, comma separated: ");
- String line = IO.readString();
- String[] items = line.split(","); //saves characters into an array by typing them seperated by commas
- String[] arr = new String[items.length];
- for (int i=0; i < arr.length; i++) {
- arr[i] = (items[i]);
- }
- System.out.print("Input array :");
- printArray(arr);
- selectionSort(arr);
- System.out.print("Sorted array:");
- printArray(arr);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement