希尔排序
描述
利用希尔排序算法实现线性表的排序。希尔排序是根据给定的增量序列将线性表分隔成某个“增量”的记录组成一个子序例,在子序列中采用直接插入排序完成。
输入
第一行为元素个数n(1<=n<=1000),第二行为n个元素值(整数),即需要排序的元素个数,第三行增量序列中增量个数m,第四行为m个增量,可以假定最后一个增量为1。
输出
对每一测试用例,用m行输出各增量进行希尔排序结果,用空格隔开。
样例输入
10
49 38 65 97 76 13 27 49 55 4
3
5 3 1
样例输出
13 27 49 55 4 49 38 65 97 76
13 4 49 38 27 49 55 65 97 76
4 13 27 38 49 49 55 65 76 97
import java.util.Scanner;
public class Main{
public static Scanner cin=new Scanner(System.in);
public static void main(String[] arg){
int n=cin.nextInt();
int[] a=new int[n];
for(int i=0;i<n;++i) a[i]=cin.nextInt();
int m=cin.nextInt();
do{
int ic=cin.nextInt();
for(int i=ic;i<n;++i){
int tmp=a[i],j=i-ic;
while(j>=0&&a[j]>tmp){
a[j+ic]=a[j];
j-=ic;
}
a[j+ic]=tmp;
}
for(int i=0;i<n;++i){
System.out.print(a[i]);
System.out.print(i<n-1?' ':'\n');
}
}
while(--m>0);
return;
}
}
直接插入排序
描述
利用直接插入排序算法实现线性表的排序。要求输出第k趟排序的结果。例如原来线性表为:26,12,25,4,36,15,21,第一趟直接排序排序结果为: 12, 26,25,4,36,15,21,第二趟直接插入排序结果为: 12,25, 26,4,36,15,21。
输入
第一个为元素个数n(1<=n<=1000),第二行为n个元素值(整数),即需要排序的元素个数,第三行为k(1<=k<=n-1),即要求的第k趟排序结果。
输出
对每一测试用例,用一行输出第k趟排序结果,用空格隔开。
样例输入
5
2 4 1 9 7
3
import java.util.Scanner;
public class Main{
public static Scanner cin=new Scanner(System.in);
public static void main(String[] arg){
int n=cin.nextInt();
int[] a=new int[n];
for(int i=0;i<n;++i) a[i]=cin.nextInt();
int k=cin.nextInt();
for(int i=1;i<=k;++i){
int j=i-1,tmp=a[i];
while(j>=0&&a[j]>tmp){
a[j+1]=a[j];
--j;
}
a[j+1]=tmp;
}
for(int i=0;i<n;++i){
System.out.print(a[i]);
if(i<n-1) System.out.print(' ');
}
return;
}
}
的核心操作是划分,通过某个数据将原来排序表分成两部分,前面部分比该数小,后面数据比该数据大或相等,该位置就为某数据排序后的位置,即该数据完成排序。
将标准输入的n个整数采用快速排序,并需要显示出每次划分分支点存储的位置,第一个数为0,分支点的输出顺序按照程序递归产生的分支点的先后进行输出,并完成该数据的排序。
输入: 标准输入,输入的第一行为整数的个数n值,第二行为n个整数,每个整数之间为一个空格。
输出: 标准输出,输出的第一行依次输出排序过程中使用的支点位置,每个输出数据之间使用一个空格隔开,第二行输出排序后的序列,每个输出数据之间使用一个空格隔开。
输入样例:
14
39 80 76 41 13 29 50 78 30 11 100 7 41 86
输出样例:
5 3 2 1 8 6 9 13 12 10
7 11 13 29 30 39 41 41 50 76 78 80 86 100
————————————————
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
sc.close();
QuickSort(arr);
display(arr);
}
// 输出数组
static void display(int[] arr) {
for (int i = 0; i < arr.length; i++) {
if (i > 0) System.out.print(" ");
System.out.print(arr[i]);
}
System.out.println();
}
// 快速排序
static void QuickSort(int[] arr) {
QuickSort(arr, 0, arr.length - 1);
System.out.println();
}
static void QuickSort(int[] arr, int left, int right){
if(left < right){
int i = partition(arr, left, right);
QuickSort(arr,left, i - 1);
QuickSort(arr, i + 1, right);
}
}
static int partition(int[] arr, int l, int r) {
int key = l;
while (l < r) {
while (l < r && arr[r] >= arr[key]) {
--r;
}
while (l < r && arr[l] <= arr[key]) {
++l;
}
if (l < r) {
swap(arr, l, r);
}
}
if (arr[key] > arr[l]) {
swap(arr, key, l);
}
System.out.print(l + " ");
return l;
}
static void swap(int[] arr, int l, int r) {
int tmp = arr[l];
arr[l] = arr[r];
arr[r] = tmp;
}
}
堆排序
堆排序的思想实际上利用完全二叉树存储在数组中,通过调整完全二叉树成为大顶堆获得一个排序表的最大值进行排序的方法,大顶堆满足根节点比子树旳节点大。堆排序主要是通过大顶堆旳根元素与未完成排序旳最后一个元素进行交换,将交换后旳完全二叉树不满足大顶堆要求调整到满足满足要求,调整通过如下方法完成:
void heapAdjust(int[] R,int s,int t);其中,数组R中存储旳二叉树,只有以R[s]为根子树,其左右子树之间可能不满足大顶堆特征。
调整堆旳操作难点为根子树节点编号为i,则左子树节点编号为2*i,右子树节点编号为2*i+1;通过比较子树旳大小选择大旳子树进行调整,一直调整到根节点比子节点大,再将根节点旳值插入到最后调整旳节点。要完成堆排序,在调整旳基础上可以通过从堆底往堆顶进行调整获得初始堆,然后通过N-1次调整完成排序,控制流程为:
void heapSort(int[] R){
int i;
int N=R.length-1;
for(i=N/2;i>0;i--){
heapAdjust(R,i,N);
}
for(i=N;i>1;i--){
R[0]=R[1];R[1]=R[i];R[i]=R[0];
heapAdjust(R,1,i-1);
}
}
给你旳问题是,将标准输入的n个整数采用堆排序,并需要显示建成旳初始堆,并完成该数据的排序。
输入:标准输入,输入的第一行为整数的个数n值,第二行为n个整数,每个整数之间为一个空格。
输出:标准输出,第一行依次输出排序过程中建成旳初始堆在数组中的存储值,每个输出数据之间使用一个空格隔开,第二行输出排序后的序列,每个输出数据之间使用一个空格隔开。
输入样例:
14
39 80 76 41 13 29 50 78 30 11 100 7 41 86
输出样例:
100 80 86 78 39 41 76 41 30 11 13 7 29 50
7 11 13 29 30 39 41 41 50 76 78 80 86 100
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] R = new int[n];
for (int i = 0; i < n; i++) {
R[i] = sc.nextInt();
}
sc.close();
heapSort(R);
display(R);
}
// 输出数组
static void display(int[] R) {
for (int i = 0; i < R.length; i++) {
if (i > 0) System.out.print(" ");
System.out.print(R[i]);
}
System.out.println();
}
// 堆排序
static void heapSort(int[] R) {
for (int i = 0; i < R.length - 1; ++i) {
int end = R.length - i;
for (int j = end / 2 - 1; j >= 0; --j) {
int current = j;
int child = current * 2 + 1;
while (child < end) {
if (child + 1 < end && R[child] < R[child + 1]) {
++child;
}
if (R[child] > R[current]) {
int tmp = R[child];
R[child] = R[current];
R[current] = tmp;
}
current = child;
child = current * 2 + 1;
}
}
if (i == 0) display(R);
int tmp = R[end - 1];
R[end - 1] = R[0];
R[0] = tmp;
}
}
}