MergeSort. Error in merging logic - java

Can anyone say what is the problem in the merging logic? If element of right array is less than element in left array then it works. Other wise gives wrong answer. I have divided the array properly. I am giving my merging logic here.
public static void main(String[] args) {
int[] list = { 32,14, 67, 76, 23, 41, 58, 85};
System.out.println("before: " + Arrays.toString(list));
mergeSort(list);
System.out.println("after: " + Arrays.toString(list));
}
public static void mergeSort(int[] array) {
if (array.length > 1) {
// split array into two halves
int[] left = leftHalf(array);
int[] right = rightHalf(array);
mergeSort(left);
mergeSort(right);
merge(array, left, right);
}
}
public static void merge(int[] result,
int[] left, int[] right) {
int i1 = 0; // index into left array
int i2 = 0; // index into right array
int j = 0;
int k = 0;
for (int i=0; i1 < left.length && i2 < right.length;i++) {
if (left[i1] < right[i2]) {
result[i] = left[i1];
// take from left
i1++;
} else {
result[i] = right[i2];
// take from right
i2++;
}
}
}

You've got two unused variables j and k in your code. Your logic is flawed because you copy into the array until one half is empty, so you never actually empty both halves.
public static void merge(int[] result, int[] left, int[] right) {
int i1 = 0; // index into left array
int i2 = 0; // index into right array
int i;
for (i = 0; i1 < left.length && i2 < right.length;i++) {
if (left[i1] < right[i2]) {
result[i] = left[i1++];
// take from left
} else {
result[i] = right[i2++];
// take from right
}
}
for (; i1 < left.length; i++) result[i] = left[i1++];
for (; i2 < right.length; i++) result[i] = right[i2++];
}
Only one of the two for loops will run at the end, and will process what's left of the other half.

Related

Given an array of n positive integers and k. I have to find min number of swaps required to bring all the numbers less than or equal to k together

Logic used to solve the question
Maintain a count variable, two pointers i and j and scan them from left to right(i) and right to left(j).
Assign i the index of first element <= k.
If i and j stop at a particular position that means they need to be exchanged and update count.
Exit the loop when i and j cross each other.
Return no of exchanges happened ; that is return count.
Question
Can anybody help me find out why count returns wrong for some cases(like the one in unit testing where it returns 4 but correct output is 3)? What's the bug here?
public class MinSwaps {
public static int minSwap(int[] arr, int n, int k) {
int count = 0;
int lo = 0;
int hi = arr.length - 1;
int i = lo - 1;
int j = hi + 1;
// assign i the position of first element <= k in arr
for (int l = 0; l < n; l++) {
if (arr[l] <= k) {
i = l;
break;
}
}
while (true) {
// keep scanning i(from left to right) until arr[i] > k
while (arr[++i] <= k) {
if (i == hi) {
break;
}
}
// keep scanning j(right to left) until arr[j] <= k
while (arr[--j] > k) {
if (j == lo) {
break;
}
}
// exit loop if i and j cross each other
if (i >= j) {
break;
}
// exchange elements at i and j ( when they have stopped )
exch(arr, i, j);
count++;
}
return count;
}
private static void exch(int[] a, int i, int j) {
int temp;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
// unit testing
public static void main(String[] args) {
int[] arr = {4, 16, 3, 8, 13, 2, 19, 4, 12, 2, 7, 17, 4, 19, 1};
StdOut.println(minSwap(arr, 15, 9));
}
}

Why is my selection sort algorithm not working?

I'm trying to make a selection sort algorithm in java that finds the smallest element of an unsorted array and puts it on the end of a new array. But my program only copies the first element twice, gets the next one, and then the rest is all zeroes:
public static int find_min(int[] a){
int m = a[0];
for (int i = 0; i < a.length; i++){
if (m > a[i])
m = a[i];
}
return m;
}
public static int[] selectionSort(int[] unsorted){
int[] c = new int[unsorted.length];
for(int i = 0; i < unsorted.length; i++){
int smallest = find_min(unsorted);
c[i] = smallest;
unsorted = Arrays.copyOfRange(unsorted, i+1, unsorted.length );
}
return c;
}
When I put in something like:
public static void main(String[] args){
int a[] = {1,-24,4,-4,6,3};
System.out.println(Arrays.toString(selectionSort(a)));
}
I get:
[-24, -24, -4, 0, 0, 0]
where is this going wrong? Is this a bad algorithm?
Ok, let's revisit the selection sort algorithm.
public static void selectionSort(int[] a) {
final int n = a.length; // to save some typing.
for (int i = 0; i < n - 1; i++) {
// i is the position where the next smallest element should go.
// Everything before i is sorted, and smaller than any element in a[i:n).
// Now you find the smallest element in subarray a[i:n).
// Actually, you need the index
// of that element, because you will swap it with a[i] later.
// Otherwise we lose a[i], which is bad.
int m = i;
for (int j = m + 1; j < n; j++) {
if (a[j] < a[m]) m = j;
}
if (m != i) {
// Only swap if a[i] is not already the smallest.
int t = a[i];
a[i] = a[m];
a[m] = t;
}
}
}
In conclusion
You don't need extra space to do selection sort
Swap elements, otherwise you lose them
Remembering the loop invariant is helpful

Merge Sort count number of swaps and compares

I have this existing code that I need to add a swap and compare counter for. So far I believe I have the counts correctly however I cannot get the output to no display a loop of each swap.
public void mergeSort(int[] a, int howMany) {
if (a.length >= 2) {
// split array into two halves
int[] left = Arrays.copyOfRange(a, 0, a.length/2);
int[] right = Arrays.copyOfRange(a, a.length/2, a.length);
// sort the two halves
mergeSort(left,howMany);
mergeSort(right, howMany);
// merge the sorted halves into a sorted whole
merge(a, left, right);
}
}
// Merges the left/right elements into a sorted result.
// Precondition: left/right are sorted
public static void merge(int[] result, int[] left,
int[] right) {
int i1 = 0; // index into left array
int i2 = 0; // index into right array
int compCount = 0;
int swapCount = 0;
for (int i = 0; i < result.length; i++) {
compCount++;
if (i2 >= right.length ||
(i1 < left.length && left[i1] <= right[i2])) {
result[i] = left[i1]; // take from left
i1++;
swapCount++;
} else {
result[i] = right[i2]; // take from right
i2++;
swapCount++;
}
}
//figure this loop issue out System.out.println("merge sort " + compCount + " " + swapCount);
}
Make a global variable or a field within the class which holds the merge and merge sort methods. This will allow the methods to increment to the variable. If you declare inside the method it will stay as a local variable and each recursive call will produce different local variables of the same name but belonging to the different recursive method calls. Therefore your code should look like:
public class ClassWithMergeMethodInside
{
int swapCount;
int compCount;
public void mergeSort(int[] a, int howMany) {
//Since merge sort begins here you may initiliaze the variables here
swapCount = 0;
compCount = 0;
if (a.length >= 2) {
// split array into two halves
int[] left = Arrays.copyOfRange(a, 0, a.length/2);
int[] right = Arrays.copyOfRange(a, a.length/2, a.length);
// sort the two halves
mergeSort(left,howMany);
mergeSort(right, howMany);
// merge the sorted halves into a sorted whole
merge(a, left, right);
}
}
// Merges the left/right elements into a sorted result.
// Precondition: left/right are sorted
public static void merge(int[] result, int[] left,
int[] right) {
int i1 = 0; // index into left array
int i2 = 0; // index into right array
//Will not declare counter variables here
for (int i = 0; i < result.length; i++) {
compCount++;
if (i2 >= right.length ||
(i1 < left.length && left[i1] <= right[i2])) {
result[i] = left[i1]; // take from left
i1++;
swapCount++;
} else {
result[i] = right[i2]; // take from right
i2++;
swapCount++;
}
}
//figure this loop issue out System.out.println("merge sort " + compCount + " " + swapCount);
}
}
I think the most elegant solution is to simulate pass-by-reference by using an int wrapper class. Everything in Java is pass-by-value, but if you don't change the reference that an Object reference points to, you can simulate pass-by-reference for recursive calls.
Here is an example:
import java.util.Arrays;
public class TestMain
{
public static void main(String[] args)
{
int[] array = new int[]{6, 2, 1, 4, 5, 3};
IntWrapper countCompare = new IntWrapper();
IntWrapper countSwap = new IntWrapper();
MergeSort mSort = new MergeSort();
mSort.mergeSort(array, countCompare, countSwap);
System.out.println("Compares: " + countCompare.val);
System.out.println("Swaps: " + countSwap.val);
for (int i = 0; i < array.length; i++){
System.out.print(String.format("%-3d", array[i]));
}
}
}
public class IntWrapper{
int val = 0;
}
public class MergeSort
{
public void mergeSort(int[] a, IntWrapper compares, IntWrapper swaps) {
if (a.length >= 2) {
// split array into two halves
int[] left = Arrays.copyOfRange(a, 0, a.length/2);
int[] right = Arrays.copyOfRange(a, a.length/2, a.length);
// sort the two halves
mergeSort(left, compares, swaps);
mergeSort(right, compares, swaps);
// merge the sorted halves into a sorted whole
merge(a, left, right, compares, swaps);
}
}
// Merges the left/right elements into a sorted result.
// Precondition: left/right are sorted
public static void merge(int[] result, int[] left, int[] right, IntWrapper compares, IntWrapper swaps) {
int i1 = 0; // index into left array
int i2 = 0; // index into right array
//int compCount = 0;
//int swapCount = 0;
for (int i = 0; i < result.length; i++) {
//compCount++;
compares.val++;
if (i2 >= right.length ||
(i1 < left.length && left[i1] <= right[i2])) {
result[i] = left[i1]; // take from left
i1++;
//swapCount++;
swaps.val++;
} else {
result[i] = right[i2]; // take from right
i2++;
//swapCount++;
swaps.val++;
}
}
//figure this loop issue out System.out.println("merge sort " + compCount + " " + swapCount);
}
}
Output:
Compares: 16
Swaps: 16
1 2 3 4 5 6

BubbleSort Implementation

I tried to make an implementation of bubble sort, but I am not sure whether it is correct or not. If you can give it a look and if it is a bubble sort and can be done in better way please don't be shy. Here is the code:
package Exercises;
import java.util.*;
public class BubbleSort_6_18
{
public static void main(String[] args)
{
Random generator = new Random();
int[] list = new int[11];
for(int i=0; i<list.length; i++)
{
list[i] = generator.nextInt(10);
}
System.out.println("Original Random array: ");
printArray(list);
bubbleSort(list);
System.out.println("\nAfter bubble sort: ");
printArray(list);
}
public static void bubbleSort(int[] list)
{
for(int i=0; i<list.length; i++)
{
for(int j=i + 1; j<list.length; j++)
{
if(list[i] > list[j])
{
int temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
}
}
public static void printArray(int[] list)
{
for(int i=0; i<list.length; i++)
{
System.out.print(list[i] + ", ");
}
}
}
private static int [] bublesort (int[] list , int length) {
boolean swap = true;
int temp;
while(swap){
swap = false;
for(int i = 0;i < list.length-1; i++){
if(list[i] > list[i+1]){
temp = list[i];
list[i] = list[i+1];
list[i+1] = temp;
swap = true;
}
}
}
return list;
}
Mohammod Hossain implementation is quite good but he does alot of unecessary iterations, sadly he didnt accept my edit and i can't comment due to reputations points so here is how it should look like:
public void sort(int[] array) {
int temp = 0;
boolean swap = true;
int range = array.length - 1;
while (swap) {
swap = false;
for (int i = 0; i < range; i++) {
if (array[i] > array[i + 1]) {
temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
swap = true;
}
}
range--;
}
}
This is the calssical implementation for bubble sort and it seems to be OK. There are several optimizations that can be done, but the overall idea is the same. Here are some ideas:
If there is an iteration of the outer cycle when no swap is performed in the inner cycle, then break, no use to continue
On each iteration of the outer cycle swap the direction of the inner one - do it once left to right and then do it once right to left(this helps avoid elements moving slowly towards the right end).
{
System.out.println("The Elments Before Sorting:");
for(i=0;i<a.length;i++)
{
System.out.print(a[i]+"\t");
}
for(i=1;i<=a.length-1;i++)
{
for(j=0;j<=a.length-i-1;j++)
{
if((a[j])>(a[j+1]))
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
System.out.println("The Elements After Sorting:");
for(i=0;i<a.length;i++)
{
System.out.println(a[i]+"\t");
}
}
}
Short Answer: This is definitely NOT Bubble sort. It is a variant of Selection sort (a less efficient variant than the commonly known one).
It might be helpful to see a visualization of how they work on VisuAlgo
Why this is not bubble sort?
Because you loop over the array and compare each element to each other element on its right. if the right element is smaller you swap. Thus, at the end of the first outer loop iteration you will have the smallest element on the left most position and you have done N swaps in the worst case (think of a reverse-ordered array).
If you think about it, you did not really need to do all these swaps, you could have searched for the minimum value on the right then after you find it you swap. This is simply the idea of Selection sort, you select the min of remaining unsorted elements and put it in its correct position.
How does bubble sort look like then?
In bubble sort you always compare two adjacent elements and bubble the larger one to the right. At the end of the first iteration of the outer loop, you would have the largest element on the right-most position. The swap flag stops the outer loop when the array is already sorted.
void bubbleSort(int[] arr) {
boolean swap = true;
for(int i = arr.length - 1; i > 0 && swap; i--) {
swap = false;
// for the unsorted part of the array, bubble the largest element to the right.
for (int j = 0; j < i; j++) {
if (arr[j] > arr[j+1]) {
// swap
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
swap = true;
}
}
}
}
Yes it seems to be Bubble sort swapping the elements
Bubble sort
void bubbleSort(int arr[])
{
int n = arr.length;
for (int i = 0; i < n-1; i++)
for (int j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
{
// swap temp and arr[i]
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
It will give in worst case O(n^2) and even if array is sorted.
I think you got the idea of bubble sort by looking at your code:
Bubble sort usually works like the following:
Assume aNumber is some random number:
for (int i = 0; i < aNumber; i++)
{
for(int j = 0; j < aNumber; j++)
//Doing something with i and j, usually running it as a loop for 2D array
//array[i][j] will give you a complete sort.
}
How bubble sort works is it iterates through every single possible spot of the array. i x j times
The down side to this is, it will take square the number of times to sort something. Not very efficient, but it does get the work done in the easiest way.
You can loop over the array until no more elements are swapped
When you put the element at the last position you know it's the largest, so you can recuce the inner loop by 1
A bubblesort version with while loops from my first undergraduate year ("the BlueJ era").
public static void bubbleSort()
{
int[] r = randomArrayDisplayer();
int i = r.length;
while(i!=0){
int j = 0;
while(j!=i-1){
if(r[j+1]<r[j]){
swap(r,j,j+1);
}
j++;
}
i--;
}
}
private static void swap(int[] r, int u, int v)
{
int value = r[u];
r[u] = r[v];
r[v] = value;
arrayDisplayer(r);
}
My advice is to display every step in order to be sure of the correct behaviour.
public class BubbleSort {
public static void main(String[] args) {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
BubbleSort client=new BubbleSort();
int[] result=client.bubbleSort(arr);
for(int i:result)
{
System.out.println(i);
}
}
public int[] bubbleSort(int[] arr)
{
int n=arr.length;
for(int i=0;i<n;i++)
{
for(int j=0;j<n-i-1;j++)
if(arr[j]>arr[j+1])
swap(arr,j,j+1);
}
return arr;
}
private int[] swap(int[] arr, int i, int j) {
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
return arr;
}
}
Above code is looks like implementation Selection sort , it's not a bubble sort.
Please find below code for bubble sort.
Class BubbleSort {
public static void main(String []args) {
int n, c, d, swap;
Scanner in = new Scanner(System.in);
System.out.println("Input number of integers to sort");
n = in.nextInt();
int array[] = new int[n];
System.out.println("Enter " + n + " integers");
for (c = 0; c < n; c++)
array[c] = in.nextInt();
for (c = 0; c < ( n - 1 ); c++) {
for (d = 0; d < n - c - 1; d++) {
if (array[d] > array[d+1]) /* For descending order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
System.out.println("Sorted list of numbers");
for (c = 0; c < n; c++)
System.out.println(array[c]);
}
}
/*
Implementation of Bubble sort using Java
*/
import java.util.Arrays;
import java.util.Scanner;
public class BubbleSort {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of elements of array");
int n = in.nextInt();
int []a = new int[n];
System.out.println("Enter the integer array");
for(int i=0; i<a.length; i++)
{
a[i]=in.nextInt();
}
System.out.println("UnSorted array: "+ Arrays.toString(a));
for(int i=0; i<n; i++)
{
for(int j=1; j<n; j++)
{
if(a[j-1]>a[j])
{
int temp = a[j-1];
a[j-1]=a[j];
a[j]=temp;
}
}
}
System.out.println("Sorted array: "+ Arrays.toString(a));
}
}
/*
****************************************
Time Complexity: O(n*n)
Space Complexity: O(1)
****************************************
*/
class BubbleSort {
public static void main(String[] args) {
int a[] = {5,4,3,2,1};
int length = a.length - 1;
for (int i = 0 ; i < length ; i++) {
for (int j = 0 ; j < length-i ; j++) {
if (a[j] > a[j+1]) {
int swap = a[j];
a[j] = a[j+1];
a[j+1] = swap;
}
}
}
for (int x : a) {
System.out.println(x);
}
}
}
int[] nums = new int[] { 6, 3, 2, 1, 7, 10, 9 };
for(int i = nums.Length-1; i>=0; i--)
for(int j = 0; j<i; j++)
{
int temp = 0;
if( nums[j] < nums[j + 1])
{
temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
}
}
package com.examplehub.sorts;
public class BubbleSort implements Sort {
/**
* BubbleSort algorithm implements.
*
* #param numbers the numbers to be sorted.
*/
public void sort(int[] numbers) {
for (int i = 0; i < numbers.length - 1; ++i) {
boolean swapped = false;
for (int j = 0; j < numbers.length - 1 - i; ++j) {
if (numbers[j] > numbers[j + 1]) {
int temp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j + 1] = temp;
swapped = true;
}
}
if (!swapped) {
break;
}
}
}
/**
* Generic BubbleSort algorithm implements.
*
* #param array the array to be sorted.
* #param <T> the class of the objects in the array.
*/
public <T extends Comparable<T>> void sort(T[] array) {
for (int i = 0; i < array.length - 1; ++i) {
boolean swapped = false;
for (int j = 0; j < array.length - 1 - i; ++j) {
if (array[j].compareTo(array[j + 1]) > 0) {
T temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
swapped = true;
}
}
if (!swapped) {
break;
}
}
}
}
source from
function bubbleSort(arr,n) {
if (n == 1) // Base case
return;
// One pass of bubble sort. After
// this pass, the largest element
// is moved (or bubbled) to end. and count++
for (let i = 0; i <n-1; i++){
if (arr[i] > arr[i + 1])
{
let temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
// Largest element is fixed,
// recur for remaining array
console.log("Bubble sort Steps ", arr, " Bubble sort array length reduce every recusrion ", n);
bubbleSort(arr, n - 1);
}
let arr1 = [64, 3400, 251, 12, 220, 11, 125]
bubbleSort(arr1, arr1.length);
console.log("Sorted array : ", arr1);
Here's an implementation for the bubble sort algorithm using Stack:
static void bubbleSort(int[] elements) {
Stack<Integer> primaryStack = new Stack<>();
Stack<Integer> secondaryStack = new Stack<>();
int lastIndex = elements.length - 1;
for (int element : elements) {
primaryStack.push(element);
} // Now all the input elements are in primaryStack
// Do the bubble sorting
for (int i = 0; i < elements.length; i++) {
if (i % 2 == 0) sort(elements, i, primaryStack, secondaryStack, lastIndex);
else sort(elements, i, secondaryStack, primaryStack, lastIndex);
}
}
private static void sort(int[] elements, int i, Stack<Integer> stackA, Stack<Integer> stackB, int lastIndex) {
while (!stackA.isEmpty()) { // Move an element from stack A to stack B
int element = stackA.pop();
if (stackB.isEmpty() || element >= stackB.peek()) { // Don't swap, just push
stackB.push(element);
} else { // Swap, then push
int temp = stackB.pop();
stackB.push(element);
stackB.push(temp);
}
}
elements[lastIndex - i] = stackB.pop();
}

combining and sorting two arrays Java?

So basically there are two separate presorted arrays, and you have to combine them and sort them (without sort() methods of course). Here is my code:
public static void main(String[] args) {
int a [] = {3,5,7,9,12,14, 15};
int b [] = {6 ,7, 10};
int j = 0;
//output array should be 3,5,6,7,7,9,10,12,14,15
int c [] = new int[a.length+b.length];//10 values
for (int i = 0;i<b.length;i++){
while(b[i]>a[j]){
c[j] = a[j] ;
j++;
}
if(b[i] == a[j]){
c[j] = b[i];
c[j+1] = a[j];
}
c[j] = b[i];
j++;
}
for(int i = 0;i<c.length;i++)
System.out.println(c[i]);
}
I'm guessing the zeros I am getting are from a mistake in one of the booleans (< & >), but I cant seem to figure it out. It works fine for the first 4, but once I get to the repeating 7's, it just goes crazy.
Please help me understand, don't just change the code.
This is how it should be in a simple way:
public static void main(String[] args) {
int a [] = {3,5,7,9,12,14, 15};
int b [] = {6 ,7, 10};
int j = 0, k = 0;
//output array should be 3,5,6,7,7,9,10,12,14,15
int c [] = new int[a.length+b.length];//10 values
// we're filling c with the next appropriate number
// we start with checking a[0] and b[0] till we add
// all the elements
for (int i = 0; i < c.length; i++) {
// if both "a" and "b" have elements left to check
if (j < a.length && k < b.length) {
// check if "b" has a smaller element
if (b[k] < a[j]) {
// if so add it to "c"
c[i] = b[k];
k++;
}
// if "a" has a smaller element
else {
// add it to "c"
c[i] = a[j];
j++;
}
}
// if there are no more elements to check in "a"
// but there are still elements to check in "b"
else if (k < b.length) {
// add those elements in "b" to "c"
c[i] = b[k];
k++;
}
// if there are no more elements to check in "b"
// but there are still elements to check in "a"
else {
// add those elements in "a" to "c"
c[i] = a[j];
j++;
}
}
for(int i = 0; i < c.length; i++)
System.out.println(c[i]);
}
Hope it helps.
You can try this code.
public static void main(String[] args) {
int a[] = { 3, 5, 7, 9, 12, 14, 15 };
int b[] = { 6, 7, 10 };
// output array should be 3,5,6,7,7,9,10,12,14,15
int alen = a.length;
int blen = b.length;
int c[] = new int[a.length + b.length];// 10 values
int s[] = null;
int[] l = null;
if (alen < blen) {
s = a;
l = b;
} else {
s = b;
l = a;
}
// Constructing Combined Array
for (int i = 0, p = 0; i < c.length; i++, p++) {
if (i == s.length) {
p = 0;
}
if (i < s.length) {
c[i] = s[p];
} else {
c[i] = l[p];
}
}
//Sorting the C array
for (int i = 1; i < c.length; i++) {
int j = i;
int B = c[i];
while ((j > 0) && (c[j - 1] > B)) {
c[j] = c[j - 1];
j--;
}
c[j] = B;
}
for (int i = 0; i < c.length; i++)
System.out.print(c[i]);
}
Actually it's better to say merging (not combining) two arrays.
Simple algorithm (taken from this article) for merging sorted arrays A and B[0..n-1] into result C[0..m+n-1]:
Introduce read-indices i, j to traverse arrays A[0..m-1] and B, accordingly. Introduce write-index k to store position of the first free cell in the resulting array. By default i = j = k = 0.
At each step: if both indices are in range (i < m and j < n), choose minimum of (A[i], B[j]) and write it to C[k]. Otherwise go to step 4.
Increase k and index of the array, algorithm located minimal value at, by one. Repeat step 2.
Copy the rest values from the array, which index is still in range, to the resulting array.
Hope it helps.
Use ai and bi for the indices in both source arrays and ci as the index for the destination array.
You only need one loop.
Try to keep this very clear and advance in c by exactly one element at each iteration.
In the loop, check wether the end of one array was reached. If so, just take an element from the other array. Otherwise, take only the smaller element of a[ai] and b[bi] and increment the corresponding index.
It is very easy to make mistakes in mergesort (or in any code where two arrays need to be walked in parallel) by thinking "hey I can go along with a while loop instead of just doing a single if", but then you typically have two loops nested in a third one, and for each of the loops you have to do the right bounds checks, and there is typically no significant gain in performance.
p.s. Doing one main loop and then two cleanup loops after the main loop is fine, just avoid nested loops if they are not necessary, in particular in interviews where this may also cause confusion when calculating the runtime.
Try this, your error is you are using the same cellular index for array A and array C:
public class MainClass {
public static void main(String[] args) {
int[] arrayA = { 23, 47, 81, 95 };
int[] arrayB = { 7, 14, 39, 55, 62, 74 };
int[] arrayC = new int[10];
merge(arrayA, arrayA.length, arrayB, arrayB.length, arrayC);
for (int i : arrayC) {
System.out.println(i);
}
}
public static void merge(int[] arrayA, int sizeA, int[] arrayB, int sizeB, int[] arrayC) {
int arrayAIndex = 0, arrayBIndex = 0, arrayCIndex = 0;
while (arrayAIndex < sizeA && arrayBIndex < sizeB)
if (arrayA[arrayAIndex] < arrayB[arrayBIndex])
arrayC[arrayCIndex++] = arrayA[arrayAIndex++];
else
arrayC[arrayCIndex++] = arrayB[arrayBIndex++];
while (arrayAIndex < sizeA)
arrayC[arrayCIndex++] = arrayA[arrayAIndex++];
while (arrayBIndex < sizeB)
arrayC[arrayCIndex++] = arrayB[arrayBIndex++];
}
}
This is another version
// size of C array must be equal or greater than
// sum of A and B arrays' sizes
public void merge(int[] A, int[] B, int[] C) {
int i, j, k, m, n;
i = 0;
j = 0;
k = 0;
m = A.length;
n = B.length;
while (i < m && j < n) {
if (A[i] <= B[j]) {
C[k] = A[i];
i++;
} else {
C[k] = B[j];
j++;
}
k++;
}
if (i < m) {
for (int p = i; p < m; p++) {
C[k] = A[p];
k++;
}
} else {
for (int p = j; p < n; p++) {
C[k] = B[p];
k++;
}
}
}
public class Combinearray {
public static void main(String[] args) {
int[] array1= {5,4,6,2,1};
int[] array2= {2,5,8,4,1,6,4};
int m=array1.length;
int n=array2.length;
int[] array3=new int[m+n];
int a=1;
for(int i=0;i<m;i++) {
array3[i]=array1[i];//array1 is copied to array3
}
for(int i=0;i<n;i++) {
array3[m-1+a]=array2[i];//array2 is copied to array3
a++;
}
//array3 is combined array
int l=array3.length;
int temp[]=new int[l];
for(int i=0;i<l;i++) {
for(int j=i+1;j<l;j++) {
if(array3[i]>array3[j]) {
temp[i]=array3[i];
array3[i]=array3[j];
array3[j]=temp[i];
}
}
}
System.out.println("sorted array is ");
System.out.print("[");
for(int i=0;i<l;i++) {
System.out.print(array3[i]+" ");
}
System.out.print("]");
}
}

Categories