Merge sort - non-recursive implementation

package ls.algorithm.sort;


 


public class MergeSort extends SortInf {
	int[] temp;
	public MergeSort(int size) {

//Here an array is randomly generated in the abstraction and printed and displayed

		super(size);
		this.printSrcArr();//
		temp=new int[size];
		copyArr(arr,temp);
		// TODO Auto-generated constructor stub
	}
	private void copyArr(int[] src,int[] target)
	{
		if(src.length!=target.length)
		{
			System.out.println("Inconsistent length array cannot be copied");
			return ;
		}
		for(int i=0;i<src.length;i++)
		{
			target[i]=src[i];
		}
	}


	@Override
	public void sort() {
		// TODO Auto-generated method stub
		int gap=1;
		//When the step size is greater than or equal to the length of the array, it means that all elements of the array have been merged last time
		while(gap<(2*arr.length))
		{
			/ / Traverse the array according to the gap length, group and merge
			for(int i=0;i<arr.length;i=i+2*gap)//Because the length of the combined single number is gap, the step size of the loop is 2*gap
			{
				mergeArr(i,gap);
			}
			//For each merge, the gap step size is 2 times the previous time
			gap=gap*2;
		}
		this.printSrcArr();
	}


	private void mergeArr(int start,int gap)
	{
		if(start+gap>=arr.length)
		{
			//There is no redundant array behind, no need to merge
			return ;
		}
		// merge adjacent arrays
		int leftStart=start;//The starting position of the left array
		int leftEnd=start+gap-1;//The tail position of the left array
		int rightStart=start+gap;//The starting position of the right array
		int rightEnd=start+gap+gap-1;//The tail position of the right array
		//If the boundary of the right array is smaller than the boundary of the source array, it is not processed; otherwise, the boundary of the right array is equal to the right boundary of the source array
		rightEnd=rightEnd<=arr.length-1?rightEnd:arr.length-1;
		// Formally merge adjacent arrays and store the data into a temporary array of equal size;
		for(int n=leftStart;n<=rightEnd;n++)
		{
			if(leftStart>leftEnd) //If the left has been read. Put the rest on the right into the temporary sequence in turn
			{
				temp[n]=arr[rightStart];
				rightStart++;
				continue;
			}
			if(rightStart>rightEnd) //If the right side has been read. Put the rest on the left into the temporary sequence in turn
			{
				temp[n]=arr[leftStart];
				leftStart++;
				continue;
			}
			if(leftStart<=leftEnd&&rightStart<=rightEnd)//If both sides are not read, continue to compare
			{
				if(arr[leftStart]<=arr[rightStart])
				{
					temp[n]=arr[leftStart];
					leftStart++;
				}
				else
				{
					temp[n]=arr[rightStart];
					rightStart++;
				}
			}		
		}
		copyArr(temp,arr); //Copy the current sequence to the original sequence
// this.printSrcArr(); //Print the current merged array
	}
}



//The following is an abstract class that randomly generates an array to be sorted.

package ls.algorithm.sort;
import java.util.Random;

public abstract class SortInf {


	int[] arr;
	public SortInf(int size)
	{
		arr=new int[size];
		for(int i=0;i<size;i++)
		{
			arr[i]=new Random().nextInt(50);
		}
	}
	public void printSrcArr()
	{
		System.out.print("Array:");
		for(int i=0;i<arr.length;i++)
		{
			System.out.print(arr[i]+" ");
		}
		System.out.print("\n");
	}
	public abstract void sort();
	
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324670392&siteId=291194637