i want check if algorithms correct quick sort method , selection sort method. believe have quick sorting correct , selection sorting correct may need correcting. how can display sorting back?
selection sorting
public class selectionsort { // complexity of o(n2) public void selectionsort(int numbers[], int array_size) { int i; int j; int min; (i = 0; < array_size - 1; i++) { min = i; (j = + 1; j < array_size; j++) { if (numbers[j] < numbers[min]) min = j; } swap(numbers[i], numbers[min]); } } public void swap(int num1, int num2) { int temp; temp = num1; num1 = num2; num2 = temp; } } quick sorting
public class quicksort { // complexity of o(n log n). public void quicksort(int numbers[], int array_size) { quicksort(numbers, 0, array_size - 1); } public void quicksort(int numbers[], int left, int right) { int pivot; int lefthold; int righthold; lefthold = left; righthold = right; pivot = numbers[left]; while (left < right) { while ((numbers[right] >= pivot) && (left < right)) right--; if (left != right) { numbers[left] = numbers[right]; left++; } while ((numbers[left] <= pivot) && (left < right)) left++; if (left != right) { numbers[right] = numbers[left]; right--; } } numbers[left] = pivot; pivot = left; left = lefthold; right = righthold; if (left < pivot) quicksort(numbers, left, pivot - 1); if (right > pivot) quicksort(numbers, pivot + 1, right); } } main class
public class sorttesting { public static void main(string[] args) { quicksort qsort = new quicksort(); selectionsort ssort = new selectionsort(); int i; int numbers[] = { 12, 9, 4, 99, 120, 1, 3, 10 }; system.out.print("values in sort:\n"); (i = 0; < numbers.length; i++) { system.out.print(numbers[i] + " "); system.out.println(); } qsort.quicksort(numbers, 1); ssort.selectionsort(numbers, 1); //display sort of quicksort //display sort of selectionsort } }
your quicksort fine,but there problem swap function in selection sort.the problem is:
in java primitives passed value.
so, if pass 2 integers swap function swap not reflected in calling function. can corrected rather passing array , indices want swap . correct swap function is:
public void swap(int []numbers,int i,int min) { int temp; temp = numbers[i]; numbers[i] = numbers[min]; numbers[min]=temp; } and correct call is:
swap(numbers,i,min); now coming main function there type error you, have mentioned array lengths 1, wrong, alwasy advisable when mentioning array lengths in java use this
array_identifier.length
also provide same unsorted array other sorting algorithm, use method clone() clone unsorted array array , send selection sort , therefore correct main class is:
public class sorttesting { public static void main(string[] args) { quicksort qsort = new quicksort(); selectionsort ssort = new selectionsort(); int i; int numbers[] = { 12, 9, 4, 99, 120, 1, 3, 10 }; system.out.print("values in sort:\n"); (i = 0; < numbers.length; i++) { system.out.print(numbers[i] + " "); system.out.println(); } int []numbers2=numbers.clone(); qsort.quicksort(numbers, numbers.length);/*mention correct array length*/ system.out.println("\nthe array after sorting using quick sort is"); for(int e:numbers) system.out.print(e+" "); ssort.selectionsort(numbers2, numbers2.length);/*mention correct array length*/ system.out.println("\nthe array after sorting using selection sort is"); for(int e:numbers2) system.out.print(e+" "); } }
Comments
Post a Comment