1. 程式人生 > >歸併,快排,堆排

歸併,快排,堆排

 

//遞迴版的歸併
package 分治;
import java.util.Scanner;

import javax.sound.midi.SysexMessage;
public class Merge1 {
	static Scanner sc = new Scanner(System.in);
	static int num;
	static public void Merge(int digit[],int left, int mid, int right)
	{
		int i = left ;
		int j = mid + 1;
		int k = 0;
		int temp[] = new int [right - left + 1];//新的這幾個元素
		while(i <= mid && j <= right)   
		{
			if(digit[i] <= digit[j])
			{
				temp[k++] = digit[i]; //從小到大排序的
				i++;
			}
			else 
			{
				temp[k++] = digit[j];
				j++;
			}
		}
		
		while(i <= mid)
		{
			temp[k++] = digit[i];
			i++;
		}
		while(j <= right)
		{
			temp[k++] = digit[j];
			j++;
		}
		int gg = 0 ;
		for(int z = left; z <= right; z++)
			digit[z] = temp[gg++];
	}
	static public void MergeSort(int digit[], int left ,int right)
	{
		if(left < right)
		{
			int mid = (left + right) / 2;
			MergeSort(digit, left, mid);
			MergeSort(digit, mid + 1, right);
			Merge(digit,left,mid,right); //這個合併
		}
	}
	public static void main(String[] args) {
		num = sc.nextInt();
		int digit[] = new int [num];
		for(int i = 0 ; i < num; i++)
			digit[i] = sc.nextInt();
		MergeSort(digit,0,num - 1);
		for(int i = 0 ; i < num; i++)
			System.out.println(i + "    "+digit[i]);
	}
}

 

//非遞迴版本的歸併
package 分治;
import java.util.Scanner;

import javax.sound.midi.SysexMessage;
public class Merge2 {
	static Scanner sc = new Scanner(System.in);
	static int num;
	static public void Merge(int digit[],int left, int mid, int right)
	{
		int i = left ;
		int j = mid + 1;
		int k = 0;
		int temp[] = new int [right - left + 1];//新的這幾個元素
		while(i <= mid && j <= right)   
		{
			if(digit[i] <= digit[j])
			{
				temp[k++] = digit[i]; //從小到大排序的
				i++;
			}
			else 
			{
				temp[k++] = digit[j];
				j++;
			}
		}
		
		while(i <= mid)
		{
			temp[k++] = digit[i];
			i++;
		}
		while(j <= right)
		{
			temp[k++] = digit[j];
			j++;
		}
		int gg = 0 ;
		for(int z = left; z <= right; z++)
			digit[z] = temp[gg++];
	}
	static void MergePass(int digit[], int cnt, int total)
	{
		int i ;
		for(i = 0 ; i + 2 * cnt - 1 <= total; i += 2 * cnt)
			Merge(digit,i,i + cnt - 1,i + 2 * cnt - 1);   
		
		if(i + cnt < total)
			Merge(digit,i, i + cnt - 1, total);
	}
	public static void main(String[] args) {
		num = sc.nextInt();
		int digit[] = new int [num];
		for(int i = 0 ; i < num; i++)
			digit[i] = sc.nextInt();
		
		int cnt = 1; //表示現在要合併多少
		while(cnt < num)
		{
			MergePass(digit, cnt , num - 1);  //起始是0 終點是num-1;
			cnt += cnt;
		}
		for(int i = 0 ; i < num; i++)
			System.out.println(i + "    "+digit[i]);
	}
}
package 快排序;
import java.util.Scanner;
public class Quicksort1 {
	static Scanner sc = new Scanner(System.in); 
	static void QuickSort(int digit[], int left, int right)
	{
		if(left < right) {
			int par = Partition(digit,left,right);
			QuickSort(digit,left, par - 1);
			QuickSort(digit,par + 1, right);
		}
	}
	static int Partition(int[] digit, int left, int right) {
		int key = digit[left];
		while (left < right) {
			while (digit[right] >= key && right > left)
				right--;
			digit[left] = digit[right];
			while (digit[left] <= key && right > left)
				left++;
			digit[right] = digit[left];
		}
		digit[left] = key;
		return left;
	}

	
	public static void main(String[]args)
	{
		int num = sc.nextInt();
		int digit[] = new int [num];
		for(int i = 0 ; i < num ; i++)
			digit[i] = sc.nextInt();
		
		QuickSort(digit, 0, num - 1);
		for(int i = 0 ; i < num; i++)
			System.out.print(digit[i] + " ");
	}
}
#include<bits/stdc++.h>
using namespace std;
int num[105];
void pushDown(int beginn,int tol)
{
	int x = num[beginn],child,i;
	for(i = beginn; i * 2 <= tol; i = child)
	{
		child = i * 2;//表示左邊的節點 
		if(child + 1 <= tol && num[child] < num[child + 1])	
			child++;
		if(num[child] > x)
			num[i] = num[child];
		else 
			break;	
	}
		num[i] = x;
	return;
}
int main()
{
	int n ;
	scanf("%d",&n);
	for(int i = 1; i <= n ; i ++)
		scanf("%d", &num[i]);
	for(int i = n/2; i >= 1; i--)
		pushDown(i,n);
	for(int i = n; i > 1; i --)
	{
		swap(num[1], num[i]);
		pushDown(1, i - 1);
	}
	for(int i = 1; i <= n ; i ++)
		printf("%d ",num[i]);
	return 0;
}