how to perform insertion sort recursively - java

I have two methods that I intend to perform an insertion sort on an array of ints. The first method does the outer loop of the insertion sort algorithm while the second method inserts the particular element in the correct position recursively. For example:
int [] list = {20, 50, 30, 10, 60, 40};
sort(list);
The result should be {10,20,30,40,50,60}
My problem is in the insert method; Here is my code:
public static void sort(int[] list) {
int position;
for (position = 1; position < list.length; position++) {
insert(list, list[position], position);
}
}
public static void insert(int[] list, int element, int position) {
int position2 = position - 1;
if (position == list.length - 1) {
// do nothing
} else {
while ((position > 0) && (list[position - 1] > element)) {
element = list[position - 1];
insert(list, element, position - 1);
}
list[position] = element;
}
}
Currently my output is,
{20, 50, 50, 50, 60, 40}
I do not completely understand recursion. Please help

When you make some function recursive, there should be a recursion end condition along with other conditions.
Run this version and try to see what is that you're doing wrong.
Try this version:
public static void insertInOrder(int element, int[] a, int first, int last) {
if (element >= a[last])
a[last + 1] = element;
else if (first < last) {
a[last + 1] = a[last];
insertInOrder(element, a, first, last - 1);
}
else // first == last and element < a[last]
{
a[last + 1] = a[last];
a[last] = element;
}
}
public static void insertion_sort_recur(int[] arr, int first, int last) {
if (first < last) {
insertion_sort_recur(arr, first, last - 1); // avoids looping thru arr[0..last-1]
insertInOrder(arr[last], arr, first, last - 1); // considers arr[last] as the first element in the unsorted list
}
}
public static void main(String args[]) {
int A[] = { 5, 3, 2, 4, 6, 1 };
insertion_sort_recur(A, 0, 5);
for(int i=0; i < A.length; i++) {
System.out.println(A[i]);
}
}
Source:
http://analgorithmaday.blogspot.com/2011/01/insertion-sortrecursive-method.html

Try below code by providing else as an integer array, with sortedIndex as index of first element and and index as index of second element:
public static void insertionSort(int[] ele, int sortedIndex, int index) {
if (sortedIndex < ele.length) {
if (index < ele.length) {
if (ele[sortedIndex] > ele[index]) {
ele[sortedIndex] += ele[index];
ele[index] = ele[sortedIndex] - ele[index];
ele[sortedIndex] = ele[sortedIndex] - ele[index];
}
insertionSort(ele, sortedIndex, index + 1);
return;
}
if (index == ele.length) {
sortedIndex++;
}
insertionSort(ele, sortedIndex, sortedIndex + 1);
}
}

Related

How do you implement quick sort to sort in descending order (java)?

This current quicksort implementation will sort an array to be in ascending order. How do I change this code so that it will sort an array to be in descending order? I need some help reversing the logic. Advice would be much appreciated.
public void quickSort(int[] array) {
// An array of size 1 is already sorted
if (array.length < 2)
return;
// Find the largest element and put it at the end of the array
int max = 0;
for (int i = 1; i < array.length; i++) {
if (array[i] > array[max]) {
max = i;
}
}
swap(array, array.length-1, max);
// Call the main quicksort method
quicksort(array, 0, array.length-1);
}
public void quicksort(int[] array, int first, int last) {
int lower = first + 1, upper = last;
// Use the middle array element as the bound (pivot) and
// move it out of the way into the first array element
swap(array, first, (first + last)/2);
int bound = array[first];
// Partition the array
while (lower <= upper) {
while (array[lower] < bound) {
lower++;
}
while (array[upper] > bound) {
upper--;
}
if (lower < upper) {
swap(array, lower++, upper--);
}
else {
lower++;
}
}
// Move the pivot into its proper position in the array
swap(array, upper, first);
// Recursively sort the lower and upper subarrays
if (first < upper-1) {
quicksort(array, first, upper-1);
}
if ((upper+1) < last) {
quicksort(array, upper+1, last);
}
}
So the basic concept would be to modify...
while (array[lower] < bound) {
lower++;
}
while (array[upper] > bound) {
upper--;
}
to match your requirements, but it's not particularly reusable. Instead, we want to delegate the comparison so that we can use the same code, but which can allow for different algorithms to be used, for example...
public class QuickSort {
public static interface Matcher {
public int compare(int lhs, int rhs);
}
public static void sort(int[] array, Matcher matcher) {
// An array of size 1 is already sorted
if (array.length < 2) {
return;
}
// Find the largest element and put it at the end of the array
int max = 0;
for (int i = 1; i < array.length; i++) {
if (matcher.compare(array[i], array[max]) > 0) {
max = 1;
}
}
swap(array, array.length - 1, max);
// Call the main quicksort method
sort(array, 0, array.length - 1, matcher);
}
protected static void sort(int[] array, int first, int last, Matcher matcher) {
int lower = first + 1, upper = last;
// Use the middle array element as the bound (pivot) and
// move it out of the way into the first array element
swap(array, first, (first + last) / 2);
int bound = array[first];
// Partition the array
while (lower <= upper) {
while (matcher.compare(array[lower], bound) < 0) {
lower++;
}
while (matcher.compare(array[upper], bound) > 0) {
upper--;
}
if (lower < upper) {
swap(array, lower++, upper--);
} else {
lower++;
}
}
// Move the pivot into its proper position in the array
swap(array, upper, first);
// Recursively sort the lower and upper subarrays
if (first < upper - 1) {
sort(array, first, upper - 1, matcher);
}
if ((upper + 1) < last) {
sort(array, upper + 1, last, matcher);
}
}
protected static void swap(int[] values, int first, int second) {
int temp = values[first];
values[first] = values[second];
values[second] = temp;
}
}
Which can then be used something like...
int[] values = new int[]{1, 0, 2, 9, 3, 8, 4, 7, 5, 6};
QuickSort.sort(values, new QuickSort.Matcher() {
#Override
public int compare(int lhs, int rhs) {
return Integer.compare(lhs, rhs);
}
});
System.out.println(Arrays.toString(values));
QuickSort.sort(values, new QuickSort.Matcher() {
#Override
public int compare(int lhs, int rhs) {
return Integer.compare(rhs, lhs);
}
});
System.out.println(Arrays.toString(values));
which will output...
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

How to output left and right block indexes in JumpSearch in Java?

I'm trying to output left and right indexes of the block in jump search algorithm, but I don't really know how to do it. Do you have any idea? The last block may be shorter so probably left border is going to be shorter than the others, so I can really picture a solution. When I'm declaring a new variable and I'm trying to update it in a loop, then outside of the loop it still has the old value and I really don't know why.
My code:
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int length = scanner.nextInt();
int[] array = new int[length];
for (int i = 0; i < array.length; i++) {
array[i] = scanner.nextInt();
}
int target = scanner.nextInt();
jumpSearch(array,target);
}
public static int jumpSearch(int[] array, int target) {
int currentRight = 0;
int prevRight = 0;
// If array is empty, the element is not found
if (array.length == 0) {
return -1;
}
// Check the first element
if (array[currentRight] == target) {
return 0;
}
// Calculating the jump length over array elements
int jumpLength = (int) Math.sqrt(array.length);
// Finding a block where the element may be present
while (currentRight < array.length - 1) {
// Calculating the right border of the following block
currentRight = Math.min(array.length - 1, currentRight + jumpLength);
if (array[currentRight] >= target) {
break; // Found a block that may contain the target element
}
prevRight = currentRight; // update the previous right block border
// If the last block is reached and it cannot contain the target value => not found
if ((currentRight == array.length - 1) && target > array[currentRight]) {
return -1;
}
/* Doing linear search in the found block */
// backwardSearch(array, target, prevRight, currentRight);
return 0;
}
public static int backwardSearch(int[] array, int target, int leftExcl, int rightIncl) {
for (int i = rightIncl; i > leftExcl; i--) {
if (array[i] == target) {
return i;
}
}
return -1;
}
}
Here is the code from the example that I linked to in my comment to your question. I added three lines to it. I added a the following comment to each line I added.
// ADDED THIS LINE
public class JumpSearch
{
public static int jumpSearch(int[] arr, int x)
{
int n = arr.length;
// Finding block size to be jumped
int step = (int)Math.floor(Math.sqrt(n));
// Finding the block where element is
// present (if it is present)
int prev = 0;
while (arr[Math.min(step, n)-1] < x)
{
prev = step;
step += (int)Math.floor(Math.sqrt(n));
if (prev >= n)
return -1;
}
// Doing a linear search for x in block
// beginning with prev.
int left = prev; // ADDED THIS LINE
int right = Math.min(step, n); // ADDED THIS LINE
while (arr[prev] < x)
{
prev++;
// If we reached next block or end of
// array, element is not present.
if (prev == Math.min(step, n))
return -1;
}
// If element is found
if (arr[prev] == x) {
System.out.printf("Found %d between %d and %d%n", x, left, right); // ADDED THIS LINE
return prev;
}
return -1;
}
// Driver program to test function
public static void main(String [ ] args)
{
int arr[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21,
34, 55, 89, 144, 233, 377, 610};
int x = 55;
// Find the index of 'x' using Jump Search
int index = jumpSearch(arr, x);
// Print the index where 'x' is located
System.out.println("\nNumber " + x +
" is at index " + index);
}
}
When I run the above code, I get this output.
Found 55 between 8 and 12
Number 55 is at index 10
Is that what you are looking for?

I am not sure what is wrong with my quick select method

So I tried to write a code to select kth smallest element in a given input integer array using quick sort, but for some reason as you can see in the code below,
public static int partition(int[] input, int first, int end) {
int pivot = input[(first + end)/2];
int i = first - 1;
int j = end + 1;
while (true) {
do {
i++;
} while (input[i] < pivot);
do {
j--;
} while (input[j] > pivot);
if (i < j)
swap(input, i, j);
else
return j;
}
}
public static void swap(int[] input, int i, int j){
int temp = input[i];
input[i] = input[j];
input[j] = temp;
}
public static int select(int[] input, int k){
return mySelect(input, 0, input.length-1, k);
}
public static int mySelect(int[] input, int left, int right, int k){
// If k is smaller than number of elements in array
if (k > 0 && k <= right - left + 1) {
int pos = partition(input, left, right);
if (pos - left == k - 1)
return input[pos];
// If position is larger, recursive call on the left subarray
if (pos - left > k - 1)
return mySelect(input, left, pos-1, k);
// if smaller, recursive call on the right subarray
return mySelect(input, pos+1, right, k-pos+left-1);
}
System.out.println("Invalid k value");
return Integer.MAX_VALUE;
}
public static void main(String[] args){
test2 = new int[]{99, 44, 77, 22, 55, 66, 11, 88, 33};
int[] test2 = new int[]{99, 44, 77, 22, 55, 66, 11, 88, 33};
//testing for selecting kth min
System.out.println("The 1st smallest : " + select(test2, 1));
System.out.println("The 2nd smallest : " + select(test2, 2));
System.out.println("The 3rd smallest : " + select(test2, 3));
System.out.println("The 4th smallest : " + select(test2, 4));
System.out.println("The 6th smallest : " + select(test2, 6));
System.out.println("The 9th smallest : " + select(test2, 9));
}
but my 1st smallest element appears 22, 2nd smallest returns 11, while others values are normal.
can someone please help me find what is the mistake I made?
The problem in your code is in the partition. The do while is the culprit here. You are updating the positions before checking the conditions and that's causing the problem with the last swap operation.
Update your method to this and you will be good to go
public static int partition(int[] input, int first, int end) {
int pivot = input[(first + end)/2];
int i = first;
int j = end;
while (true) {
while (input[i] < pivot) {
i++;
}
while (input[j] > pivot) {
j--;
}
if (i < j)
swap(input, i, j);
else
return j;
}
}
I wrote this code while I was learning quick sort. The mistake I made was in not realizing that
'left' and 'right' are indices in array whereas,
'pivot' is one of the values stored in array
You can study my code below and determine what's wrong in your understanding of the algorithm!!
public class QuickSort
{
public static void main(String[] args)
{
int[] nums = {1,5,9,2,7,8,4,2,5,8,9,12,35,21,34,22,1,45};
quickSort(0,nums.length-1,nums);
//print nums to see if sort is working as expected,omit printing in your case
print(nums);
//now you can write code to select kth smallest number from sorted nums here
}
/**
* left and right are indices in array whereas
* pivot is one of the values stored in array
*/
static int partition(int left, int right , int pivot, int[] nums)
{
int leftIndex = left -1;
int rightIndex = right;
int temp = 0;
while(true)
{
while( nums[++leftIndex] < pivot );
while( rightIndex>0 && nums[--rightIndex] > pivot );
if( leftIndex >= rightIndex ) break;
else//swap value at leftIndex and rightIndex
{
temp = nums[leftIndex];
nums[leftIndex]= nums[rightIndex];
nums[rightIndex] = temp;
}
}
//swap value at leftIndex and initial right index
temp = nums[leftIndex];
nums[leftIndex]= nums[right];
nums[right] = temp;
return leftIndex;
}
static void quickSort( int leftIndex , int rightIndex ,int[] nums)
{
if( rightIndex-leftIndex <= 0 ) return;
else
{
int pivot = nums[rightIndex];
int partitionIndex = partition(leftIndex, rightIndex , pivot,nums);
quickSort(leftIndex,partitionIndex-1,nums);
quickSort(partitionIndex+1,rightIndex,nums);
}
}
static void print(int[] nums)
{
for( int i = 0 ; i < nums.length ; i++ )
{
System.out.print(nums[i]+", ");
}
}
}

sorting using heapsort in java

import java.util.Arrays;
public class Test{
//main method
public static void main(String... args){
int[] a = new int[]{16,14,10,8,7,9,3,2,4,1};
System.out.println("unsorted array " + Arrays.toString(a));
heap_sort(a);
System.out.println("Sorted array " + Arrays.toString(a));
}
//returns left child index of given index
private static int left_child(int i){
return (i * 2 + 1);
}
//returns right child index of the given index
private static int right_child(int i){
return (i * 2 + 2);
}
public static void max_heapify(int[] array , int index , int heap_size){
int largest = index;
int left = left_child(index);
int right = right_child(index);
if(left < heap_size && array[left] > array[index]){
largest = left;
}
else largest = index;
if (right < heap_size && array[right] > array[largest]){
largest = right;
}
if(largest != index){
//exchange array[index] with array[largest]
int a = array[index];
array[index] = array[largest];
array[largest] = a;
max_heapify(array,largest,heap_size);
}
}
public static void build_max_heap(int[] array){
int heap_size = array.length - 1;
for (int i = ((array.length - 1) / 2) ; i >= 0 ; i--){
max_heapify(array, i, heap_size);
}
}
//main heap sort function
public static void heap_sort(int[] array){
int heap_size = array.length - 1;
build_max_heap(array);
for(int i = (array.length - 1) ; i > 0 ; i--){
//exchange array[0] with array[i]
int a = array[0];
array[0] = array[i];
array[i] = a;
heap_size = heap_size - 1;
max_heapify(array , 1 , heap_size);
}
}
}
I have tried almost every possibility but it doesn't works fine .Could u find out where i am wrong.
Output of my code is :
unsorted array [16, 14, 10, 8, 7, 9, 3, 2, 4, 1]
Sorted array [14, 10, 8, 7, 9, 3, 2, 4, 1, 16]
There are some problems:
- Left and right child are modified because you loose the first element in the array.
- In max_heapify function, the loops are <=
- In heap_sort function you must pass 0 instead of 1 to the max_heapify function.
//returns left child index of given index
private static int left_child(int i)
{
return (i * 2);
}
//returns right child index of the given index
private static int right_child(int i)
{
return (i * 2 + 1);
}
public static void max_heapify(int[] array, int index, int heap_size)
{
int largest = index;
int left = left_child(index);
int right = right_child(index);
if (left <= heap_size && array[left] > array[index])
{
largest = left;
}
if (right <= heap_size && array[right] > array[largest])
{
largest = right;
}
if (largest != index)
{
//exchange array[index] with array[largest]
int a = array[index];
array[index] = array[largest];
array[largest] = a;
max_heapify(array, largest, heap_size);
}
}
public static void build_max_heap(int[] array)
{
int heap_size = array.length - 1;
for (int i = ((array.length - 1) / 2); i >= 0; i--)
{
max_heapify(array, i, heap_size);
}
}
//main heap sort function
public static void heap_sort(int[] array)
{
int heap_size = array.Length - 1;
build_max_heap(array);
for (int i = (array.Length - 1); i > 0; i--)
{
//exchange array[0] with array[i]
int a = array[0];
array[0] = array[i];
array[i] = a;
heap_size = heap_size - 1;
max_heapify(array, 0, heap_size);
}
}
The problem is that you loose items because the left and right child must be:
//returns left child index of given index
private static int left_child(int i){
return (i * 2);
}
//returns right child index of the given index
private static int right_child(int i){
return (i * 2 + 1);
}

Quick Sort not sorting iterative solution

I currently have the below code for my quick sort. As you can see it is not producing the correct output. Any help will be greatly appreciated. I need the pivot to be the first item in the array. As you can also see i am measuring the time it takes to run, but please ignore that for the time being.
edit: to be specific it works correctly when i have a list in descending order and ascending order(already sorted), but when i try a random list it does not work.
Output:
QuickSort:
Quick Sort Took 181023 nanoseconds
Quick Sort Took 0 milliseconds
Sorted Quick Sort: 25, 12, 17, 13, 20, 7, 5, 16, 11, 26, 24, 18, 9, 4, 21, 1, 23, 27, 15, 19, 28, 14, 8, 22, 6, 3, 2, 10, 29, 40, 37, 32, 44, 38, 35, 41, 39, 31, 42, 30, 43, 36, 34, 33, 45, 46, 47, 48, 49, 50,
Code:
class QuickSort {
public static int Partition(int[] numbers, int left, int right){
//selects the first item at position [0] as the pivot
//left is given 0 when the method is called
int pivot = numbers[left];
while (true)
{
while (numbers[left] < pivot)
left++;
while (numbers[right] > pivot)
right--;
if (left < right)
{
int temp = numbers[right];
numbers[right] = numbers[left];
numbers[left] = temp;
}
else
{
return right;
}
}
}
//method to check for special cases
public static void QuickSort_Check(int[] numbers, int left, int right)
{
//special case of 2 items to be sorted
if(right == 1){
if(numbers[0] >= numbers[1]){
System.out.print("This is a special case of 2 inputs: ");
System.out.print(numbers[1] + ", " + numbers[0]);
System.exit(0);
}
else {
System.out.print("This is a special case of 2 inputs: ");
System.out.print(numbers[0] + ", " + numbers[1]);
System.exit(0);
}
System.exit(0);
}
//special case of 1 item to be sorted
else if (right == 0){
System.out.print("This is a special case of 1 input: ");
System.out.print(numbers[0]);
System.exit(0);
}
else {
QuickSort_Iterative(numbers, left, right);
}
}
public static class QuickPosInfo
{
public int left;
public int right;
};
public static QuickPosInfo spot = new QuickPosInfo();
public static void QuickSort_Iterative(int[] numbers, int left, int right)
{
if(left >= right)
return; // Invalid index range
LinkedList<QuickPosInfo> list = new LinkedList< QuickPosInfo>();
spot.left = left;
spot.right = right;
list.add(spot);
while(true)
{
if(list.size() == 0)
break;
left = list.get(0).left;
right = list.get(0).right;
list.remove(0);
int pivot = Partition(numbers, left, right);
if(pivot > 1)
{
spot.left = left;
spot.right = pivot - 1;
list.add(spot);
}
if(pivot + 1 < right)
{
spot.left = pivot + 1;
spot.right = right;
list.add(spot);
}
}
}
}
This partition method correctly sorted half the array, then did not sort the other half so I believe the problem is in your QuickSort_Iterative(); when the pivot equals 21 it just infinitely swaps 20 and 21.
private static int partition(int[] arr,int left,int right) {
int pivot = arr[right];
int small = left-1;
for(int k = left;k < right;k++)
{
if(arr[right] <= pivot)
{
small++;
swap(arr,right,small);
}
}
swap(arr,right,small+1);
System.out.println("Pivot= "+arr[small+1]);//prints out every sort
System.out.println(Arrays.toString(arr));
return small+1;
}
private static void swap(int[] arr, int k, int small) {//easy swap method
int temp;
temp = arr[k];
arr[k] = arr[small];
arr[small] = temp;
}
UPDATE
Here is the requested method. I believe that the problem with your original one is that you are not modifying that values of left and right properly as the array is sorted.
void QuickSort(int arr[], int left, int right)
{
// create auxiliary stack
int stack[] = new int[right-l+1];
// initialize top of stack
int top = -1;
// push initial values in the stack
stack[++top] = left;
stack[++top] = right;
// keep popping elements until stack is not empty
while (top >= 0)
{
// pop right and l
right = stack[top--];
left = stack[top--];
// set pivot element at it's proper position
int p = partition(arr, left, right);
// If there are elements on left side of pivot,
// then push left side to stack
if ( p-1 > left )
{
stack[ ++top ] = l;
stack[ ++top ] = p - 1;
}
// If there are elements on right side of pivot,
// then push right side to stack
if ( p+1 < right )
{
stack[ ++top ] = p + 1;
stack[ ++top ] = right;
}
}
}
I don't know that will help you out or not you can also try the below code for same.
public class Demo {
private int array[];
private int length;
public void sort(int[] inputArr) {
if (inputArr == null || inputArr.length == 0) {
return;
}
this.array = inputArr;
length = inputArr.length;
quickSort(0, length - 1);
}
private void quickSort(int lowerIndex, int higherIndex) {
int i = lowerIndex;
int j = higherIndex;
int pivot = array[lowerIndex+(higherIndex-lowerIndex)/2];
while (i <= j) {
while (array[i] < pivot) {
i++;
}
while (array[j] > pivot) {
j--;
}
if (i <= j) {
exchangeNumbers(i, j);
i++;
j--;
}
}
if (lowerIndex < j)
quickSort(lowerIndex, j);
if (i < higherIndex)
quickSort(i, higherIndex);
}
private void exchangeNumbers(int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
public static void main(String a[]){
Demo sorter = new Demo();
int[] input = {8,693,29,3,2,8,29,82,4,26,2,62,82,6,52,9,42,6,52,66,2,8};
sorter.sort(input);
for(int i:input){
System.out.print(i);
System.out.print(" ");
}
}
}

Categories

Resources