分治法_快速排序(另外含随机快速排序)

今天,再次分享分治法的第三个经典算法:快速排序(含随机快速排序)
需要的朋友请自取哦~
欢迎大家与我一起交流呀~~
一起学习,一起进步!

<
public class quickSort {

static Comparable[] a;//待排数组

//交换两元素类
	static class MyMath {
		public static void swap(Comparable[] a, int i, int j) {
			Comparable c=a[i];
			a[i]=a[j];
			a[j]=c;
		}
	}
	
	//快速排序算法
 static  void qSort(int p,int r) {
	if(p<r) {
		int q=partition(p,r);
		qSort(p,q-1);//左半段排序
		qSort(q+1,r);//右半段排序
		
	}
}
static int partition(int p, int r) {//将基准元素排好位置
	Comparable x =a[p];//基准元素
	int i=p,j=r+1;
	while(true) {
		while(a[++i].compareTo(x)<0&&i<r);//找到比基准元素大的
		while(a[--j].compareTo(x)>0);//找到比基准元素小的
		if(i>=j)break;
		MyMath.swap(a,i,j);//交换
	}
	a[p]=a[j];
	a[j]=x;
	
	return j;
}
public static void main(String[] args) {
	a=new Comparable[8];
	a[0]=8;
	a[1]=4;
	a[2]=3;
	a[3]=7;
	a[4]=1;
	a[5]=5;
	a[6]=6;
	a[7]=2;
	System.out.println("快速排序前:");
	for(int i=0;i<8;i++) {
		System.out.print(a[i]+" ");
	}
	System.out.println();
	qSort(0,7);//快排
	System.out.println("快速排序后:");
	for(int i=0;i<8;i++) {
		System.out.print(a[i]+" ");
	}

}

}

运行结果:
在这里插入图片描述

由于快速排序算法的性能与划分是否对称有关,所以以下设计随机化的快速排序算法解决划分对称性问题

<
package 分治法_快速排序;

public class RandomQuitSort {

static Comparable[] a;//待排数组

//交换两元素类
	static class MyMath {
		public static void swap(Comparable[] a, int i, int j) {
			Comparable c=a[i];
			a[i]=a[j];
			a[j]=c;
		}
	}
	
	//随机快速排序算法
 static  void randomqSort(int p,int r) {
	if(p<r) {
		int q=randompartition(p,r);
		randomqSort(p,q-1);//左半段排序
		randomqSort(q+1,r);//右半段排序
		
	}
}
 //随机选取基准元素
private static int randompartition(int p, int r) {
		int i=(int)(r+1*Math.random())/1;//产生随机选取的基准元素的下标
		MyMath.swap(a, i, p);//将随机产生的元素与第一个元素交换,作为新的基准元素
		return partition(p,r);//基准元素排到相应位置
	}
static int partition(int p, int r) {//将基准元素排好位置
	Comparable x =a[p];//基准元素
	int i=p,j=r+1;
	while(true) {
		while(a[++i].compareTo(x)<0&&i<r);//找到比基准元素大的
		while(a[--j].compareTo(x)>0);//找到比基准元素小的
		if(i>=j)break;
		MyMath.swap(a,i,j);//交换
	}
	a[p]=a[j];
	a[j]=x;
	
	return j;
}
public static void main(String[] args) {
	a=new Comparable[8];
	a[0]=8;
	a[1]=4;
	a[2]=3;
	a[3]=7;
	a[4]=1;
	a[5]=5;
	a[6]=6;
	a[7]=2;
	System.out.println("随机快速排序前:");
	for(int i=0;i<8;i++) {
		System.out.print(a[i]+" ");
	}
	System.out.println();
	randomqSort(0,7);//快排
	System.out.println("随机快速排序后:");
	for(int i=0;i<8;i++) {
		System.out.print(a[i]+" ");
	}

}

}

运行结果:
在这里插入图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是使用Python实现快速排序可视化的代码,基于分治法实现: ```python import pygame import random # Initialize Pygame pygame.init() # Set the window dimensions WINDOW_WIDTH = 800 WINDOW_HEIGHT = 600 # Set the colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) # Set the font font = pygame.font.SysFont('Calibri', 25, True, False) # Set the screen screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) pygame.display.set_caption("Quick Sort Visualization") # Set the clock clock = pygame.time.Clock() # Set the array array = [random.randint(10, 500) for i in range(50)] # Set the pivot color PIVOT_COLOR = BLUE # Set the partition color PARTITION_COLOR = GREEN # Set the sorted color SORTED_COLOR = RED # Set the unsorted color UNSORTED_COLOR = WHITE # Set the function for quick_sort def quick_sort(array, start, end): if start < end: pivot_index = partition(array, start, end) quick_sort(array, start, pivot_index - 1) quick_sort(array, pivot_index + 1, end) # Set the function for partition def partition(array, start, end): pivot_index = start pivot_value = array[end] for i in range(start, end): if array[i] < pivot_value: array[i], array[pivot_index] = array[pivot_index], array[i] pivot_index += 1 array[pivot_index], array[end] = array[end], array[pivot_index] return pivot_index # Set the function for draw_text def draw_text(text, color, x, y): text_surface = font.render(text, True, color) screen.blit(text_surface, (x, y)) # Set the function for draw_array def draw_array(array): for i in range(len(array)): pygame.draw.rect(screen, UNSORTED_COLOR, (i * 15 + 50, WINDOW_HEIGHT - array[i], 10, array[i])) # Set the function for draw_partition def draw_partition(array, start, end): for i in range(start, end + 1): pygame.draw.rect(screen, PARTITION_COLOR, (i * 15 + 50, WINDOW_HEIGHT - array[i], 10, array[i])) # Set the function for draw_sorted def draw_sorted(array, start, end): for i in range(start, end + 1): pygame.draw.rect(screen, SORTED_COLOR, (i * 15 + 50, WINDOW_HEIGHT - array[i], 10, array[i])) # Set the function for draw_pivot def draw_pivot(array, pivot_index): pygame.draw.rect(screen, PIVOT_COLOR, (pivot_index * 15 + 50, WINDOW_HEIGHT - array[pivot_index], 10, array[pivot_index])) # Set the function for main def main(): # Set the initial values start = 0 end = len(array) - 1 quick_sort(array, start, end) done = False pivot_index = -1 while not done: # Check for events for event in pygame.event.get(): if event.type == pygame.QUIT: done = True # Clear the screen screen.fill(BLACK) # Draw the text draw_text("Quick Sort Visualization", WHITE, 50, 25) # Draw the array draw_array(array) # Draw the partition if pivot_index != -1: draw_partition(array, start, pivot_index - 1) draw_partition(array, pivot_index + 1, end) # Draw the sorted if pivot_index != -1: draw_sorted(array, start, pivot_index - 1) draw_sorted(array, pivot_index + 1, end) # Draw the pivot if pivot_index != -1: draw_pivot(array, pivot_index) # Update the screen pygame.display.flip() # Delay the loop clock.tick(30) # Call quick_sort if pivot_index == -1: pivot_index = start elif pivot_index == end: pivot_index = -1 elif array[pivot_index] > array[pivot_index + 1]: array[pivot_index], array[pivot_index + 1] = array[pivot_index + 1], array[pivot_index] pivot_index += 1 else: pivot_index += 1 # Call main if __name__ == '__main__': main() ``` 这个可视化程序使用Pygame库绘制图形,生成一个包随机整数的列表并将其进行快速排序。在每一步排序中,程序将突出显示分区中的元素、排序的元素和枢轴元素,并在排序过程中更新显示。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值