MergeSort index - java

I made a mergesort algorithm in java. It works, but it I don't know why.
Here's the problem:
for(int k = 0; k < r; k++)
If i write the for loop like that, the code will not work. However, if I change the 0 to "p-1", it will. The value of p is 1, which means that p-1 is 0. Why does p-1 work when 0 doesn't?
Here's the rest of my code:
public class MergeSort {
public static void main(String args[]){
Random rand = new Random();
int[] array = new int[10];
for(int i = 0; i <array.length; i++){
array[i] = rand.nextInt(11);
}
System.out.println(Arrays.toString(array));
mergeSort(array, 1, array.length);
System.out.println(Arrays.toString(array));
}
public static void merge(int[] A, int p, int q, int r){
//
//length of subarray 1
int n1 = q-p+1;
//length of subarray 2
int n2 = r-q;
int[] L = new int[n1+1];
int[] R = new int[n2+1];
for(int i = 0; i < n1; i++){
L[i] = A[p+i-1];
}
for(int j=0; j< n2; j++){
R[j] = A[q+j];
}
L[n1] = Integer.MAX_VALUE;
R[n2] = Integer.MAX_VALUE;
int i = 0;
int j = 0;
for(int k = p-1; k < r; k++){
if(L[i] <= R[j]){
A[k] = L[i];
i++;
}
else{
A[k] = R[j];
j++;
}
}
}
public static void mergeSort(int[] A, int p, int r){
if (p<r){
int q = (int) Math.floor((r+p)/2);
mergeSort(A, p, q);
mergeSort(A, q+1, r);
merge(A, p, q, r);
}
}

Consider the following example
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
and the code that you have provided
public static void mergeSort(int[] A, int p, int r){
if (p<r){
int q = (int) Math.floor((r+p)/2);
mergeSort(A, p, q);
mergeSort(A, q+1, r);
merge(A, p, q, r);
}
}
Your list is partitioned into left and right sections each with different indices.
Therefore, in this particular example, you will get
mergeSort(A, 1, 5)
mergeSort(A, 6, 10)
For the first method call, it will be processing [1, 2, 3, 4, 5, 6, 7, 8, 9]
while the second method call, it will be processing [1, 2, 3, 4, 5, 6, 7, 8, 9]
These operations are performed on the same array. In the subsequent call of merge Sort in mergeSort(A, 6, 9)
you will get
mergeSort(A, 6, 7)
mergeSort(A, 8, 9)
You can see that your indices clearly doesn't start from 0. Therefore processing it from 0 will give you the wrong result.
Here is a nice resource for you to learn about merge sort developed by the University that I am currently studying in.
visualgo-sorting

Related

Simple quicksort java implementation while maintaining partition element order

TL;DR What is the simplest way to maintain left and right partition ordering?
SO...
I am working on this HackerRank challenge, but I am having trouble with their printing requirements as the challenge states...
Please maintain the original order of the elements in the left and right partitions while partitioning around a pivot element.
...I am trying to write the most simple / elegant solution I can (prefer to not create new arrays / lists, rotate multiple elements, etc.) and here's what I have...
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
final int n;
final int[] arr;
try(final Scanner scanner = new Scanner(System.in)) {
n = scanner.nextInt();
arr = new int[n];
for(int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
}
quickSort(arr, 0, n - 1);
}
private static void quickSort(final int[] arr, final int start, final int end) {
if(start >= end) return;
final int partitionIndex = partition(arr, start, end);
quickSort(arr, start, partitionIndex - 1);
quickSort(arr, partitionIndex + 1, end);
for(int i = start; i <= end; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
private static int partition(final int[] arr, final int start, final int end) {
final int pivot = arr[start];
int pivotIndex = end + 1;
for(int i = end; i > start; i--) {
if(arr[i] > pivot) {
pivotIndex--;
final int temp = arr[pivotIndex];
arr[pivotIndex] = arr[i];
arr[i] = temp;
}
}
pivotIndex--;
arr[start] = arr[pivotIndex];
arr[pivotIndex] = pivot;
return pivotIndex;
}
}
...and my submission fails because my first partition is not {1, 3, 2, 5, 8, 7, 9} but rather {3, 2, 1, 5, 8, 7, 9}, so upon subsequent partitions, my first merge is 1 2 instead of 2 3 because my algorithm did not keep the left partition's elements ordered (i.e. 1, 3, 2).
My algorithm iterates from the end to the start, excluding the start element (the pivot)...
{5, 8, 1, 3, 7, 9, 2} -> Pivot is 5, pivot index is 7 (out of bounds)
{5, 8, 1, 3, 7, 9, 2} -> 2 is not bigger than 5, skip
{5, 8, 1, 3, 7, 9, 2} -> 9 is bigger than 5, pivot index is 6, swap 9 and 2
{5, 8, 1, 3, 7, 2, 9} -> 7 is bigger than 5, pivot index is 5, swap 7 and 2
{5, 8, 1, 3, 2, 7, 9} -> 3 is not bigger than 5, skip
{5, 8, 1, 3, 2, 7, 9} -> 1 is not bigger than 5, skip
{5, 8, 1, 3, 2, 7, 9} -> 8 is bigger than 5, pivot index is 4, swap 8 and 2
{5, 2, 1, 3, 8, 7, 9} -> pivot index is 3, swap 5 and 3
{3, 2, 1, 5, 8, 7, 9} -> final result after first partition
...I maintained the order for the right partition (i.e. 8 7 9) but not the left...
TL;DR from Wikipedia
Efficient implementations of Quicksort are not a stable sort, meaning that the relative order of equal sort items is not preserved.
SO...
Unfortunately I had to make a concession in order to make my Quicksort implementation stable; in case anyone is interested, here's what I decided to do (opting for simplicity over performance)...
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
final int n;
final int[] arr;
try(final Scanner scanner = new Scanner(System.in)) {
n = scanner.nextInt();
arr = new int[n];
for(int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
}
quickSort(arr, 0, n - 1);
}
private static void quickSort(final int[] arr, final int start, final int end) {
if(start >= end) return;
final int partitionIndex = partition(arr, start, end);
quickSort(arr, start, partitionIndex - 1);
quickSort(arr, partitionIndex + 1, end);
for(int i = start; i <= end; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
private static int partition(final int[] arr, final int start, final int end) {
final int pivot = arr[start];
int pivotIndex = start;
for(int i = start + 1; i <= end; i++) {
if(arr[i] < pivot) {
pivotIndex++;
}
}
int smallerIndex = start;
int biggerIndex = pivotIndex + 1;
final int[] copy = Arrays.copyOf(arr, arr.length);
for(int i = start + 1; i <= end; i++) {
if(copy[i] < pivot) {
arr[smallerIndex++] = copy[i];
} else if(arr[i] > pivot) {
arr[biggerIndex++] = copy[i];
}
}
arr[pivotIndex] = pivot;
return pivotIndex;
}
}

merge sort in k parts

I'd really appreciate some help on this assignment.
The task is develop a merge sort algorithm that "splits" the input array recursively into k arrays. I initially coded two methods to merge sort with two parts, namely merge sort and merge. I tried to generalize this algorithm
public class MergeSort {
public static int[] mergeSort(int[] a, int p, int r) {
// p is left bound
// r is right bound
if (p < r) {
int q = (int)Math.floor((p + r) / 2);
mergeSort(a, p, q);
mergeSort(a, q + 1, r);
return merge(a, p, q, r);
} else
return a;
}
// p ist linke grenze
// r ist rechte grenze
public static int[] merge(int[] a, int p, int q, int r) {
int n1 = q - p + 1; //length of first array
int n2 = r - q; //length of second array
int[] lt = new int[n1 + 1];
int[] rt = new int[n2 + 1];
for (int i = 0; i < n1; i++) {
lt[i] = a[p + i];
}
for (int j = 0; j < n2; j++) {
rt[j] = a[q + j + 1];
}
lt[n1] = 1000000000; //sentinels
rt[n2] = 1000000000;
int i = 0;
int j = 0;
for (int k = p; k <= r; k++) { //comparing the values of the arrays and merging
if (lt[i] <= rt[j]) {
a[k] = lt[i];
i++;
} else {
a[k] = rt[j];
j++;
}
}
return a;
}
public static int[] mergeSortK(int[] a, int k, int p, int r) {
// k number of steps; p is first index of array; r is last index of array;
if (p < r) {
int[] pos = new int[k + 1]; //array for saving the indices of the "splits"
for (int i = 0; i <= k; i++) {
pos[i] = (int) Math.floor(p + (r - p) / k * i); //saving the array indices
}
for (int i = 0; i < k; i++) {
mergeSortK(a, k, pos[i], pos[i + 1]); //sorting the arrays
}
for (int i = 0; i < k - 1; i++) {
merge(a, pos[i], pos[i + 1], pos[i + 2]); //merging the arrays pairwise
}
}
return a;
}
public static void main(String[] args) {
// task 2.1.a)
// Example Values:
int[] list = { 2, 1, 5, 6, 2, 12 };
int k = 4;
// use MergeSort
int[] newlist = mergeSortK(list, k, 0, list.length);
printList(newlist);
}
// Helper function to print the elements of a list
private static void printList(int[] list) {
for(int i = 0; i < list.length; i++) {
System.out.println(list[i]);
}
}
}
The input given in the main method results in {2, 1, 2, 5, 6, 12}
Any help is immensely appreciated! Sorry if I'm doing some mistakes, I'm here to learn and I really hope you guys can help me out!
There are a few problems in your code:
there is no need for Math.floor() to truncate the results of integer division, unlike Javascript, Java uses different semantics for / for int arguments and floating point arguments. (p + r) / 2 is the integral quotient, But you might want to write p + (r - p) / 2 to avoid potential overflow on p + r.
the upper index is excluded as you pass list.length from main(). This is actually a very convenient convention to avoid the need for +1 adjustments when computing slice sizes. Remove those erroneous adjustments and rely on the included/excluded convention.
don't use sentinels: using sentinels prevents you from correctly sorting arrays containing values greater or equal to the sentinel value 1000000000. This approach is not necessary and should be banned. Just compare the index variables to the slice lengths and copy the remaining elements when one of the slices is exhausted.
your computation for the slice boundaries in mergeSortK is incorrect: p + (r - p) / k * i is computed with integer arithmetics so (r - p) / k is rounded before the multiplication. The last slice ending index will not equal r if r - p is not a multiple of k. Multiplying before the division would solve this issue but might overflow the range of type int.
mergeSortK does not perform k-way merging, but a series of partial merges that are insufficient for k > 2.
your test set is a bit small.
Here is a corrected version:
public class MergeSort {
public static int[] mergeSort(int[] a, int p, int r) {
// p is left bound (included)
// r is right bound (excluded)
if (r - p >= 2) {
int q = p - (r - p) / 2;
mergeSort(a, p, q);
mergeSort(a, q, r);
return merge(a, p, q, r);
} else {
return a;
}
}
// p is left bound (included)
// q is start of right slice
// r is end of right slice (excluded)
public static int[] merge(int[] a, int p, int q, int r) {
int n1 = q - p; // length of first array
int n2 = r - q; // length of second array
int[] lt = new int[n1];
for (int i = 0; i < n1; i++) {
lt[i] = a[p + i];
}
int i = 0; // index into lt
int j = q; // index into a for right slice
int k = p; // index into a for merged list
while (i < n1 && j < r) { //comparing the values of the arrays and merging
if (lt[i] <= a[j]) {
a[k] = lt[i];
i++;
k++;
} else {
a[k] = a[j];
j++;
k++;
}
}
while (i < n1) { // copy remaining elements from right slice
a[k] = lt[i];
i++;
k++;
}
// remaining elements from right slice are already in place
return a;
}
public static int[] mergeSortK(int[] a, int k, int p, int r) {
// k amount of steps; p is first index of slice; r is last index of slice (excluded);
if (r - p >= 2) {
if (k > r - p)
k = r - p;
int[] pos = new int[k + 1]; //array for saving the indices of the "splits"
for (int i = 0; i <= k; i++) {
pos[i] = p + (r - p) * i / k; //saving the array indices
}
for (int i = 0; i < k; i++) {
mergeSortK(a, k, pos[i], pos[i + 1]); //sorting the arrays
}
while (k > 1) {
int i, n = 1;
for (i = 0; i < k - 1; i += 2) {
// merge slices 2 at a time: this will produce the expected output
// but is not a direct k-way merge.
merge(a, pos[i], pos[i + 1], pos[i + 2]);
pos[n++] = pos[i + 2];
}
if (i < k)
pos[n++] = pos[i + 1];
k = n - 1;
}
}
return a;
}
public static void main(String[] args) {
// task 2.1.a)
// Example Values:
int[] list = {
64, 36, 46, 31, 45, 52, 4, 48, 74, 59,
12, 16, 70, 67, 71, 26, 73, 34, 46, 84,
60, 16, 26, 68, 56, 57, 97, 6, 39, 74,
25, 69, 29, 69, 77, 26, 44, 53, 20, 6,
77, 31, 71, 91, 28, 6, 24, 75, 26, 33,
3, 20, 55, 94, 17, 81, 88, 32, 94, 32,
3, 90, 76, 69, 9, 96, 76, 53, 78, 14,
97, 32, 17, 15, 61, 63, 21, 0, 16, 14,
61, 4, 81, 86, 29, 29, 27, 57, 85, 5,
91, 54, 6, 68, 40, 88, 41, 9, 90, 51 };
int k = 4; // must be at least 2
// use MergeSort
int[] newlist = mergeSortK(list, k, 0, list.length);
printList(newlist);
}
// Helper function to print the elements of a list
private static void printList(int[] list) {
for (int i = 0; i < list.length; i++) {
System.out.println(list[i]);
}
}
}
I did not write a correct k-way merging phase at the end of mergeSortK, the code above should work but will merge the k slices in ceil(log2(k)) passes. Direct one pass k-way merging is tricky and usually not worth it.

Inserting an integer into a sorted array

I'm trying to write a method that takes a sorted array and an integer, creates a new array that is 1 size larger, and inserts the new integer and keeps it sorted.
I've tried a few different implementations and had them to work - but for this specific one, I can't grasp where it's going wrong.
int[] array1 = new int[]{1, 2, 3, 4, 6, 7, 8};
int[] printArray = insert(array1, 5);
are the arrays, and the method is
public static int[] insert(int[] a, int k) {
int[] s = new int[a.length + 1];
for(int i = 0; i < a.length; i++) {
if(k < s[i]) {
s[i] = k;
for(int j = i + 1; j < s.length; j++) {
s[j] = a[i];
i++;
}
return s;
} else {
s[i] = a[i];
}
}
return s;
}
This method prints out 1, 2, 3, 4, 6, 7, 8, 0, instead of 1, 2, 3, 4, 5, 6, 7, 8.
Change
if(k < s[i]) {
to
if(k < a[i]) {
in line 4.
Actually in the following line you are creating an array with all elements zero:
int[] s = new int[a.length + 1];
s[0] to s[7] will be 0.
Your loop counter i runs from 0 to a.length but the point to note is all the elements of array s will be zero. You are comparing k with s[i] which is zero and for that reason the shifting of arrays never happen (if block never executes).
You need to do two things to fix it.
Initialize element zero of s with the value of a[0].
Compare element with previous element to figure out position to insert.
The final code is:
public static int[] insert(int[] a, int k) {
int[] s = new int[a.length + 1];
s[0] = a[0];
for(int i = 1; i < a.length; i++) {
if(k < s[i-1]) {
s[i] = k;
for(int j = i + 1; j < s.length; j++) {
s[j] = a[i];
i++;
}
return s;
} else {
s[i] = a[i];
}
}
return s;
}
Now you will get:
[1, 2, 3, 4, 6, 5, 7, 8]
I would start by using Arrays.copyOf(int[], int) to create a new array that is one larger than the input. Then iterate that array until I reached the index that the new value belongs at. Then use System.arraycopy(Object,int,Object,int,int) to copy the values into the end of the array. That might look something like
public static int[] insert(int[] a, int k) {
int[] s = Arrays.copyOf(a, a.length + 1);
for (int i = 0; i < s.length; i++) {
if (a[i] < k) {
continue;
}
System.arraycopy(a, i, s, i + 1, s.length - i - 1);
s[i] = k;
break;
}
return s;
}
Which I tested with Arrays.toString(int[]) like
public static void main(String s[]) throws IOException {
int[] array1 = new int[] { 1, 2, 3, 4, 6, 7, 8 };
int[] printArray = insert(array1, 5);
System.out.println(Arrays.toString(printArray));
}
And I get the (expected)
[1, 2, 3, 4, 5, 6, 7, 8]

How to rotate an array?

I have the following problem to test:
Rotate an array of n elements to the right by k steps.
For instance, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to
[5,6,7,1,2,3,4]. How many different ways do you know to solve this problem?
My solution in intermediate array:
With Space is O(n) and time is O(n), I can create a new array and then copy elements to the new array. Then change the original array by using System.arraycopy().
public void rotate(int[] nums, int k) {
if (k > nums.length)
k = k % nums.length;
int[] result = new int[nums.length];
for (int i = 0; i < k; i++) {
result[i] = nums[nums.length - k + i];
}
int j = 0;
for (int i = k; i < nums.length; i++) {
result[i] = nums[j];
j++;
}
System.arraycopy(result, 0, nums, 0, nums.length);
}
But is there a better way we can do it with bubble rotate (like bubble sort) in O(1) space?
Method 1 - The Reversal Algorithm(Good One):
Algorithm:
rotate(arr[], d, n)
reverse(arr[], l, n);
reverse(arr[], 1, n-d) ;
reverse(arr[], n - d + 1, n);
Let AB are the two parts of the input array where A = arr[0..n-d-1] and B = arr[n-d..n-1]. The idea of the algorithm is:
Reverse all to get (AB) r = BrAr.
Reverse A to get BrA. /* Ar is reverse of A */
Reverse B to get BA. /* Br is reverse of B */
For arr[] = [1, 2, 3, 4, 5, 6, 7], d =2 and n = 7
A = [1, 2, 3, 4, 5] and B = [ 6, 7]
Reverse all, we get BrAr = [7, 6, 5, 4, 3, 2, 1]
Reverse A, we get ArB = [7, 6, 1, 2, 3, 4, 5]
Reverse B, we get ArBr = [6, 7, 5, 4, 3, 1, 2]
Here is the Code Snippet:
void righttRotate(int arr[], int d, int n)
{
reverseArray(arr, 0, n-1);
reverseArray(arr, 0, n-d-1);
reverseArray(arr, n-d, n-1);
}
void reverseArray(int arr[], int start, int end)
{
int i;
int temp;
while(start < end)
{
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
Method 2 - A Juggling Algorithm
Divide the array in different sets where number of sets is equal to GCD of n and d and move the elements within sets.
If GCD is 1, then elements will be moved within one set only, we just start with temp = arr[0] and keep moving arr[I+d] to arr[I] and finally store temp at the right place.
Here is an example for n =12 and d = 3. GCD is 3 and
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
Elements are first moved in first set
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12}
Then in second set.
arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12}
Finally in third set.
arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}
Here is the code:
void leftRotate(int arr[], int d, int n)
{
int i, j, k, temp;
int gcd = gcd(d, n);
for (i = 0; i < gcd; i++)
{
/* move i-th values of blocks */
temp = arr[i];
j = i;
while(1)
{
k = j + d;
if (k >= n)
k = k - n;
if (k == i)
break;
arr[j] = arr[k];
j = k;
}
arr[j] = temp;
}
}
int gcd(int a,int b)
{
if(b==0)
return a;
else
return gcd(b, a%b);
}
Time complexity: O(n)
Auxiliary Space: O(1)
Method 3 - Rotate one by one:
righttRotate(arr[], d, n)
start
For i = 0 to i < d
Right rotate all elements of arr[] by one
end
To rotate by one, store arr[n-1] in a temporary variable temp, move arr[1] to arr[2], arr[2] to arr[3] …and finally temp to arr[0]
Let us take the same example arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, rotate arr[] by one 2 times. We get [7, 1, 2, 3, 4, 5, 6] after first rotation and [ 6, 7, 1, 2, 3, 4, 5] after second rotation.
Her is Code Snippet:
void leftRotate(int arr[], int d, int n)
{
int i;
for (i = 0; i < d; i++)
leftRotatebyOne(arr, n);
}
void leftRotatebyOne(int arr[], int n)
{
int i, temp;
temp = arr[n-n];
for (i = 0; i < n-1; i++)
arr[i] = arr[i+1];
arr[n - 1] = temp;
}
Time complexity: O(n*d)
Auxiliary Space: O(1)
The following code will do your job. This is for right rotate.
public void rightrotate(int[] nums, int k) {
k %= nums.length;
reverse(nums, 0, nums.length - 1);
reverse(nums, 0, k - 1);
reverse(nums, k, nums.length - 1);
}
public void reverse(int[] nums, int start, int end) {
while (start < end) {
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}
}
If you want to do left rotate just use the following
public void leftrotate(int[] nums, int k) {
k %= nums.length;
reverse(nums, 0, k - 1);
reverse(nums, k, nums.length - 1);
reverse(nums, 0, nums.length - 1);
}
When k is negative, it rotates to the left.
Space is O(1) and time is O(n)
static void rotate(int[] num, int k) {
int n = num.length;
k = k % n;
if (k < 0) k += n;
int[] result = new int[n];
System.arraycopy(num, 0, result, k, n - k);
System.arraycopy(num, n - k, result, 0, k);
System.arraycopy(result, 0, num, 0, n);
}
ArrayUtil class is used to provide following utilities in primitive array
swap array elements
reverse array between startIndex and endIndex
leftRotate array by shift
Algorithm for array rotation by shift-
If we have to reverse array by shift value then take mod(%) with array length so that shift will become smaller than array length.
Reverse array between index 0 and shift-1
Reverse array between index shift and length-1.
Reverse complete array between index 0 and length-1.
Space Complexity: In-place Algorithm, No extra space needed so O(1).
Time Complexity : Array reversal of size k take O(k/2) i.e swapping k/2 pairs of elements.
Array Reversal time- O(k) for k size array.
Total time in Rotation-
O(1) ..........for step 1
O(shift) ......for step 2
O(n - shift) ...for step 3
O(n) ...........for step 4
Total Time for array Rotation: O(1) + O(shift) + O(n-shift) + O(n) = O(n)
public class Solution {
public static void main(String[] args) {
int k = 3;
int a[] = {1,2,3,4,5,6,7};
ArrayUtil.leftRotate(a, k);
for (int i : a)
System.out.println(i);
}
}
class ArrayUtil {
public static final boolean checkIndexOutOfRange(int[] array, int index) {
if (index < 0 || index > array.length)
return true;
return false;
}
public static final void swap(int[] array, int i, int j) {
if (checkIndexOutOfRange(array, i) || checkIndexOutOfRange(array, j))
return;
int t = array[i];
array[i] = array[j];
array[j] = t;
}
public static final void reverse(int[] array, int startIndex, int endIndex) {
if (checkIndexOutOfRange(array, startIndex) || checkIndexOutOfRange(array, endIndex))
return;
while (startIndex < endIndex) {
swap(array, startIndex, endIndex);
startIndex++;
endIndex--;
}
}
public static final void reverse(int[] array) {
reverse(array, 0, array.length - 1);
}
public static final void leftRotate(int[] array, int shift) {
int arrayLength = array.length;
if (shift >= arrayLength)
shift %= arrayLength;
reverse(array, 0, shift - 1);
reverse(array, shift, arrayLength - 1);
reverse(array);
}
}
Partial Code for ONE time array rotation
last=number_holder[n-1];
first=number_holder[0];
//rotation
number_holder[0]=last;
for(i=1;i<n;i++)
{
last=number_holder[i];
number_holder[i]=first;
first=last;
}
Display the array
for(i=1;i<n;i++)
{
System.out.println(number_holder[i]);
}
AFAIK, there are three ways to rotate an array with O(1) extra space, or put it another way, to swap two contiguous subarray.
reverse approach. reverse both part, then reverse all. most easy to code.
successively swap two contiguous block, until all items are in place.
juggling rotate, shell sort like. -- worse cache performance.
C++ has builtin function std::rotate(), which takes three iterator first, middle, last,
and return new_middle, which is where the old first element lies in the rotated
sequence.
I have checked the implementation on my computer, which use second approach I listed above.
(line 1246 in /usr/lib/gcc/i686-pc-cygwin/5.4.0/include/c++/bits/stl_algo.h).
Below is my implementation of rotate, with test program.
#include <iostream>
#include <vector>
// same logic with STL implementation, but simpler, since no return value needed.
template <typename Iterator>
void rotate_by_gcd_like_swap(Iterator first, Iterator mid, Iterator last) {
if (first == mid) return;
Iterator old = mid;
for (; mid != last;) {
std::iter_swap(first, mid);
++first, ++mid;
if (first == old) old = mid; // left half exhausted
else if (mid == last) mid = old;
}
}
// same logic with STL implementation
template <typename Iterator>
Iterator rotate_by_gcd_like_swap_then_return_new_mid(Iterator first, Iterator mid, Iterator last) {
if (first == mid) return last;
if (mid == last) return first;
Iterator old = mid;
for(;;) {
std::iter_swap(first, mid);
++first, ++mid;
if (first == old) old = mid;
if (mid == last) break;
}
Iterator result = first; // when first time `mid == last`, the position of `first` is the new `mid`.
for (mid = old; mid != last;) {
std::iter_swap(first, mid);
++first, ++mid;
if (first == old) old = mid;
else if (mid == last) mid = old;
}
return result;
}
int main() {
using std::cout;
std::vector<int> v {0,1,2,3,4,5,6,7,8,9};
cout << "before rotate: ";
for (auto x: v) cout << x << ' '; cout << '\n';
int k = 7;
rotate_by_gcd_like_swap(v.begin(), v.begin() + k, v.end());
cout << " after rotate: ";
for (auto x: v) cout << x << ' '; cout << '\n';
cout << "sz = " << v.size() << ", k = " << k << '\n';
}
Above solutions talk about shifting array elements either by reversing them or any other alternative.
I've unique solution. How about determining the starting position of element after n rotations. Once we know that, then simply insert elements from that index and increment counter using modulus operation. Using this method we can avoid using extra array operations and so on.
Here is my code:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
void rotateLeft(int n,int r) {
vector<long int> vec(n);
int j = n;
// get the position of starting index after r left rotations.
while(r!=0) {
--j;
if(j==0)
j = n;
--r;
}
for(long int i=0;i<n;++i) {
// simply read the input from there and increment j using modulus operator.
cin>>vec[j];
j = (j+1)%n;
}
// print the array
for(long int i=0;i<n;++i)
cout<<vec[i]<<" ";
}
int rotateRight (int n,int r) {
// get the position of starting index after r left rotations.
int j = r % n;
vector<long int> vec(n);
for(int i=0;i<n;i++) {
cin>>vec[j];
j=(j+1)%n;
}
for(int i=0;i<n;i++)
cout<<vec[i]<<" ";
}
int main() {
long int n,r; // n stands from number of elements in array and r stands for rotations.
cin>>n>>r;
// Time Complexity: O(n+r) Space Complexity: O(1)
rotateLeft(n,r);
// Time Complexity: O(n) Space Complexity: O(1)
rotateRight(n,r);
return 0;
}
Python code:
def reverse(arr,start , end):
while(start <= end):
arr[start] , arr[end] = arr[end] , arr[start]
start = start+1
end = end-1
arr = [1,2,3,4,5,6,7]
n = 7
k = 2
reverse(arr,0,n-1)
# [7,6,5,4,3,2,1]
reverse(arr,0,n-1-k)
# [3,4,5,6,7,2,1]
reverse(arr,n-k,n-1)
# [3,4,5,6,7,1,2]
print arr
# [3, 4, 5, 6, 7, 8, 9, 1, 2]
In Ruby Its very simple, Please take a look, Its one line.
def array_rotate(arr)
i, j = arr.length - 1, 0
arr[j],arr[i], i, j = arr[i], arr[j], i - 1, j + 1 while(j<arr.length/2)
puts "#{arr}"
end
Input: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
Output: [20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
1.using a temp array and O(n) time
public static void rotateAnArrayUsingTemp(int arr[], int d, int n) {
int temp[] = new int[d];
int tempIndex = 0;
for (int i = 0; i < d; i++) {
temp[i] = arr[i];
}
for (int i = 0; i < arr.length - d; i++) {
arr[i] = arr[i + d];
}
for (int i = arr.length - d; i < arr.length; i++) {
arr[i] = temp[tempIndex++];
}
}
This is a simple solution to rotate an array.
public class ArrayRotate {
public int[] rotateArray(int array[], int k) {
int newArray[] = new int[array.length];
for (int i = 0; i < array.length; i++) {
newArray[(i + k) % array.length] = array[i];
}
System.arraycopy(newArray, 0, array, 0, array.length);
return newArray;
}
public static void main(String[] args) {
int array[] = { 1, 2, 3, 4, 5, 6, 7 };
ArrayRotate rotate = new ArrayRotate();
rotate.display(rotate.rotateArray(array, 3));
}
public void display(int array[]) {
for (int i : array) {
System.out.print(i + ",");
}
}
}
Runtime complexity is O(n)
There are several other algorithm to achieve the same.
using temp array
Rotate One By one
Juggling algorithm
reversal method
This solution is O(1) space and O(N) time. It is in C#, takes an array parameter and rotates it in place. The algorithm goes through the first s (the shift) elements, starting with the first element moves it to the s_th position, then moves the s_th to the 2s_th position etc. If each of the first s elements rotates back to itself then there will be (arrayLength / s) * s = arrayLength loops, and at the end the array will be rotated by s. If the first s elements do not rotate back themselves, then there will still be cycles, say if s = 4, there could be one cycle which is 1-3-1 and the second 2-4-2, the line - if (ind == indAtBeg), checks for a cycle and terminates the while loop. The variable loopCount increments, when there is a rotation starting at any of the first s elements.
public static void rotateArrayByS(int[] ar, int s)
{
int len = ar.Length, ind = 0, temp1 = ar[0],
temp2 /*temp1 and temp2 for switching elements*/,
loopCount /*rotations starting at the first s elemtns of ar*/ = 0;
s %= len;
while (loopCount < s)
{
int indAtBeg = ind;
temp1 = ar[ind];
bool done = false;
while (!done)
{
if (ind < s)
loopCount++;
ind = (ind + s) % len;
//cycle detected
if (ind == indAtBeg)
done = true;
//switch the elements
temp2 = ar[ind];
ar[ind] = temp1;
temp1 = temp2;
}
++ind;
}
}
#include <stdio.h>
int
main(void)
{
int arr[7] = {1,2,3,4,5,6,7};
int new_arr[7] = {0};
int k = 3;
int len = 7;
int i=0;
for (i = (len-1); i>=0; i--) {
if ((i+k) >= len) {
new_arr[(i+k-len)] = arr[i];
} else {
new_arr[(i+k)] = arr[i];
}
}
for (i=0;i<len;i++) {
printf("%d ", new_arr[i]);
}
return 0;
}
Time complexity O(n)
Space complexity O(2*n).
Thanks.
Here is the complete Java code for left and right array rotation by k steps
import java.util.*;
public class ArrayRotation {
private static Scanner sc;
public static void main(String[] args) {
int n,k;
sc = new Scanner(System.in);
System.out.print("Enter the size of array: ");
n = sc.nextInt();
int[] a = new int[n];
System.out.print("Enter the "+n+" elements in the list: ");
for(int i=0;i<n;i++)
a[i] = sc.nextInt();
System.out.print("Enter the number of left shifts to array: ");
k = sc.nextInt();
System.out.print("Array before "+k+" shifts: ");
display(a);
leftRoation(a,k);
System.out.println();
System.out.print("Array after "+k+" left shifts: ");
display(a);
rightRoation(a,k);
System.out.println();
System.out.print("Array after "+k+" right shifts: ");
display(a);
}
public static void leftRoation(int[] a, int k){
int temp=0, j;
for(int i=0;i<k;i++){
temp = a[0];
// j=0; // both codes work i.e. for loop and while loop as well
// while(j<a.length-1){
// a[j]=a[j+1];
// j++;
// }
for(j=0;j<a.length-1;j++)
a[j]=a[j+1];
a[j]=temp;
}
}
public static void rightRoation(int[] a, int k){
int temp=0, j;
for(int i=0;i<k;i++){
temp = a[a.length-1];
for(j=a.length-1;j>0;j--)
a[j]=a[j-1];
a[j]=temp;
}
}
public static void display(int[] a){
for(int i=0;i<a.length;i++)
System.out.print(a[i]+" ");
}
}
/****************** Output ********************
Enter the size of array: 5
Enter the 5 elements in the list: 1 2 3 4 5
Enter the number of left and right shifts to array: 2
Array before 2 shifts: 1 2 3 4 5
Array after 2 left shifts: 3 4 5 1 2
Array after 2 right shifts: 1 2 3 4 5 // here the left shifted array is taken as input and hence after right shift it looks same as original array.
**********************************************/
My solution... (a: the array, n : size of array, k: number of shifts) :
public static int[] arrayLeftRotation(int[] a, int n, int k) {
if (k == 0) return a;
for (int i = 0; i < k; i++) {
int retenue = a[0];
int[] copie = java.util.Arrays.copyOfRange(a, 1, n );
for (int y = 0; y <= copie.length - 1 ; y++) {
a[y] = copie[y];
}
a[n-1] = retenue;
}
return a;
}
Java implementation for right rotation
public int[] solution(int[] A, int K) {
int len = A.length;
//Create an empty array with same length as A
int arr[] = new int[len];
for (int i = 0; i < len; i++) {
int nextIndex = i + K;
if (nextIndex >= len) {
// wraps the nextIndex by same number of K steps
nextIndex = nextIndex % len;
}
arr[nextIndex] = A[i];
}
return arr;
}
>>> k = 3
>>> arr = [1,2,3,4,5,6,7]
>>> actual_rot = k % len(arr)
>>> left_ar = arr[:-actual_rot]
>>> right_ar = arr[-actual_rot:]
>>> result = right_ar + left_ar
>>> result
[5, 6, 7, 1, 2, 3, 4]
A better way to rotate an array by k steps is:
a = [1,2,3,4,5,6]
b = a[:]
k = 2
for i in range(len(a)):
a[(i + k) % len(a)] = b[i]## (rotate right by k steps)
#a[(i - k) % len(a)] = b[i]## (rotate left by k steps)
print(a)
o/p:
[6, 5, 1, 2, 3, 4]
how to rotate an array, IN this function first argument - array, the second argument is
a number or integer.
def rotLeft(a, d):
data = a
n = d
get = data[n:len(data)]
remains = data[0:n]
data.clear()
for i in get:
data.append(i)
for x in remains:
data.append(x)
return data
This is rotating the array to the right by k steps, where k is non-negative
for (int i = 0; i < k; i++) {
for (int j = nums.length - 1; j > 0; j--) {
int temp = nums[j];
nums[j] = nums[j - 1];
nums[j - 1] = temp;
}
}
return nums;
if (k > arr.length) {
k = k % arr.length;
}
int n = arr.length - k;
int count = 0;
outer:
for (int i = arr.length - 1; i >= n; i--) {
int temp = arr[i];
inner:
for (int j = i - 1; j >= 0; j--) {
arr[j + 1] = arr[j];
if (j == 0) {
int temp2 = arr[j];
arr[j] = temp;
i = arr.length;
count++;
if (count == k) {
break outer;
}
}
}
}
Here I have solved the same problem in go.
Try to run in go playground...
sample code.
func rotate(a []int, k int) {
for j := 0; j < k ; j++ {
temp := a[len(a)-1]
for i := len(a) - 1; i > 0; i-- {
a[i] = a[i-1]
}
a[0] = temp
}
}
If you are looking for the soltuion of Codility - Cyclic Rotation Problem then, here is the JavaScript code which gave 100% for me.
function solution(A, K) {
const L = A.length - (K % A.length); // to get the rotation length value below array length (since rotation of product of array length gives same array)
const A1 = A.slice(L); // last part of array which need to be get to front after L times rotation
const A2 = A.slice(0, L); // part which rotate L times to right side
const Result = [...A1, ...A2]; // reverse and join both array by spreading
return Result;
}
Rotate an array of n elements to the right by k steps.
For instance, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].
In JS the solution will be 2 part, in two line:
function rotateArray(array,k){
// remove the rotation part
const splice = [...array].splice(0,k); //... for make a clone;
// add reversed version of the what left
return array.concat(splice.reverse()) from original array.
}

I'm having trouble getting MergeSort to work correctly

So far I have the following code:
public class MergeSort {
int[] theList;
int counter; //required for analysis later
double time; //required for analysis later
MergeSort(int[] n){
int len = n.length;
this.theList = mergeSort(n, 0, len);
}
int[] mergeSort(int[] n, int p, int r){
if((p + 1 )<r){
int q = (r + p)/2;
mergeSort(n, p, q);
mergeSort(n, q+1, r);
merge(n, p, q, r);
}
return n;
}
public void merge(int[] input, int p, int q, int r){
int lefti = q - p;
int righti = r - q;
int[] left = new int[lefti+1];
int[] right = new int[righti+1];
for(int i = 0; i < lefti; i++){
this.counter++;
left[i] = input[p+i];
}
for(int i = 0; i < righti; i++){
this.counter++;
right[i] = input[q+i];
}
left[left.length - 1] = 1000000;
right[right.length - 1] = 1000000;
int i = 0;
int j = 0;
for(int k = p; k < r; k++){
this.counter++;
if(left[i] < right[j]){
input[k] = left[i];
i++;
}
else{
input[k] = right[j];
j++;
}
}
}
}
I think I've been staring at this too long.I would appreciate if someone would be able to steer me in a better direction.
Right now, with the input of MergeSort([9, 8, 7, 6, 5, 4, 3, 2, 1, 0]), theList equals[4, 1, 0, 2, 3, 7, 5, 6, 8, 9].
I'm thankful for any help that I may receive.
It looks like you need a base case for your mergeSort method. If (p+1) >= r then sort the sub-array (i.e. the sub-array should have a size of 1 or 2, if it has a size of 1 then do nothing and if it has a size of 2 then swap the two values if they are out of order)

Categories

Resources