Question

Enter an array of length n and rank it in ascending order, ie for any adjacent two numbers a[i], a[i+1], a[i] <= a[i+1].

Sample

Input

The first line is an integer n indicating the length of the number
The next n lines, each line an integer a[i], represent the contents of the array.

Output

Output first behavior array length n
Next n acts as a result of sorting.

Result

链接: link.

图片:
2108 排序100
2108 排序100

Code of Shell Sort and Merge Sort

This is Shell Sort

// ShellSort
import java.util.Scanner;
class ShellSOrt {
	public int[] Sort(int a[]) {
		int len=a.length,gap=len/2,temp;
		while(gap>0) {
			for(int i=gap;i<len;i++) {
				int j=i;
				while(j>=gap&&a[j-gap]>a[j]) {
					temp=a[j];
					a[j]=a[j-gap];
					a[j-gap]=temp;
					j=j-gap;
					
				}
				
			}
			gap=gap/2;
		}
		return a;
		
	}

}


public class TestOfIt {
	public static void main(String args[]) {
		
		Scanner reader=new Scanner(System.in);
		int num=reader.nextInt();
		int a[]=new int[num];
		for(int i=0;i<a.length;i++) {
			a[i]=reader.nextInt();
		}
		ShellSOrt S1=new ShellSOrt();
		int a1[]=S1.Sort(a);
		System.out.println(num);
		for(int j=0;j<a1.length;j++) {
			System.out.println(a[j]);
		}
		
	}

}






The next one is Merge sort

import java.util.Scanner;
class MergeSort {
	public void mergeSort(int []a) {
		int[] workSpace=new int[a.length];
		recMergeSort(a,workSpace,0,a.length-1);
	}
	private void recMergeSort(int []a,int []b,int low,int high) {
		if(low==high)
			return;
		else {
			int middle=(low+high)/2;
			recMergeSort(a,b,low,middle);//左边排序
			recMergeSort(a,b,middle+1,high);//右边排序
			merge(a,b,low,middle+1,high);
		}
		
	}
	private void merge(int a[],int workSpace[],int lowPtr,int highPtr,int higher) {
		int j=0;
		int lower=lowPtr;
		int mid=highPtr-1;
		int n=higher-lower+1;
	
		while(lowPtr<=mid&&highPtr<=higher) {
			if(a[lowPtr]<a[highPtr]) {
				workSpace[j++]=a[lowPtr++];
			}
			else {
				workSpace[j++]=a[highPtr++];
			}
		}while(lowPtr<=mid) {
			workSpace[j++]=a[lowPtr++];
			
		}
		while(highPtr<=higher) {
			workSpace[j++]=a[highPtr++];
		}
		for(j=0;j<n;j++) {
			a[lower+j]=workSpace[j];
		}
	}
}
public class TestOfIt {
	public static void main(String args[]) {
		
		Scanner reader=new Scanner(System.in);
		int num=reader.nextInt();
		int a[]=new int[num];
		for(int i=0;i<a.length;i++) {
			a[i]=reader.nextInt();
		}
		ShellSOrt S1=new ShellSOrt();
		int a1[]=S1.Sort(a);
		System.out.println(num);
		for(int j=0;j<a1.length;j++) {
			System.out.println(a[j]);
		}
		
	}

}
public class TestOfIt {
	public static void main(String args[]) {
		
		Scanner reader=new Scanner(System.in);
		int num=reader.nextInt();
		int a[]=new int[num];
		for(int i=0;i<a.length;i++) {
			a[i]=reader.nextInt();
		}
		ShellSOrt S1=new ShellSOrt();
		int a1[]=S1.Sort(a);
		System.out.println(num);
		for(int j=0;j<a1.length;j++) {
			System.out.println(a[j]);
		}
		
	}

}


Summary

Shell sort is a sort which need a gap, exchange a[i] and a[i+gap],in general, the gap is a.length/2, after the first sort, gap need to be divided by 2 again till gap=0.divide to groups the number of group is length/2 in general.
Different from Shell sort. Merge sort adopts the idea of divide and conquer,the algorithm needs to divide an array till it is divided by many arrays which has only a number, and then, sort to new array.
The difference is Shell sort needs a gap,the number which need to sort depend on gap. However Merge sort just need to sort the numbers nearby.

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-17
  • 2021-12-13
  • 2022-12-23
  • 2022-01-06
猜你喜欢
  • 2021-08-28
  • 2022-01-21
  • 2022-02-24
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案