排序专题一

1、选择排序

import java.util.Scanner ;

public class Selection{                         //选择排序
	public static void selectionSort(Comparable[] a){
		int n = a.length ;
		for (int i=0;i<n-1;i++) {
			int min = i ;
			for (int j=i+1;j<n;j++) {
				if (less(a[j],a[min])) {
					min = j ;
				}
			}
			exch(a,i,min) ;
		}
	}

	private static boolean less(Comparable v, Comparable w){  //判断大小
		if (v.compareTo(w)<0) {
			return true ;
		}else{
			return false ;
		}
	}

	private static void exch(Comparable[] a, int i, int j){  //交换元素 ;
		Comparable temp = a[i] ;
		a[i] = a[j] ;
		a[j] = temp ;
	}

	private static void show(Comparable[] a){       //打印出来
		for (int i=0;i<a.length;i++) {
			System.out.print(a[i] + " ") ;
		}
		System.out.println() ;
	}

	public static boolean isSorted(Comparable[] a){  //判断是否已排序;
		for(int i=1;i<a.length;i++){
			if (less(a[i],a[i-1])) {
				return false ;
			}
		}
		return true ;
	}

	public static void main(String[] args){//测试;

		System.out.println("please input the length of array:") ;

		Scanner sc = new Scanner(System.in) ;
		int len = sc.nextInt() ;
		String[] a = new String[len] ;

		System.out.println("please input the numbers of array:") ;
		for (int i=0;i<len;i++) {
			a[i] = sc.next() ;
		}

		if (!isSorted(a)) {
			selectionSort(a) ;
		}

		show(a) ;
	}
}

2、冒泡排序

import java.util.Scanner ;

public class Bubble{           //冒泡排序;
	public static void bubbleSort(Comparable[] a){
		int n = a.length ;
		for (int i=1;i<n;i++) {
			for (int j=0;j<n-i;j++) {
				if (less(a[j+1],a[j])) {
					exch(a,j,j+1) ;
				}
			}
		}
	}

	private static boolean less(Comparable v, Comparable w){ //判断元素大小;
		if (v.compareTo(w)<0) {
			return true ;
		}else{
			return false ;
		}
	}

	private static void exch(Comparable[] a, int i, int j){   //交换元素;
		Comparable temp = a[i] ;
		a[i] = a[j] ;
		a[j] = temp ;
	}

	private static void show(Comparable[] a){	//打印;
		for (int i=0;i<a.length;i++) {
			System.out.print(a[i] + " ") ;
		}
		System.out.println() ;
	}

	public static boolean isSorted(Comparable[] a){   //判断是否已排序;
		for(int i=1;i<a.length;i++){
			if (less(a[i],a[i-1])) {
				return false ;
			}
		}
		return true ;
	}

	public static void main(String[] args){//测试;

		System.out.println("please input the length of array:") ;

		Scanner sc = new Scanner(System.in) ;
		int len = sc.nextInt() ;
		String[] a = new String[len] ;

		System.out.println("please input the numbers of array:") ;
		for (int i=0;i<len;i++) {
			a[i] = sc.next() ;
		}

		if (!isSorted(a)) {
			bubbleSort(a) ;
		}

		show(a) ;
	}
}

3、插入排序

import java.util.Scanner ;

public class Insertion{            //插入排序;
	public static void insertionSort(Comparable[] a){          //方法一;
		int n = a.length ;
		for (int i=1;i<n;i++) {
			for (int j=i;j>0 && less(a[j],a[j-1]);j--) {
				exch(a,j,j-1) ;
			}
		}
	}

	public static void insertionSort02(Comparable[] a){        //方法二;
		int n = a.length ;
		for (int i=1;i<n;i++) {
			Comparable temp = a[i] ;
			int j = i ;
			for (j=i-1;j>=0 && less(temp,a[j]);j--) {
				a[j+1] = a[j] ;
			}
			a[j+1] = temp ;
		}
	}

	private static boolean less(Comparable v, Comparable w){  //判断元素大小;
		if (v.compareTo(w)<0) {
			return true ;
		}else{
			return false ;
		}
	}

	private static void exch(Comparable[] a, int i, int j){   //交换元素;
		Comparable temp = a[i] ;
		a[i] = a[j] ;
		a[j] = temp ;
	}

	private static void show(Comparable[] a){	    //打印;
		for (int i=0;i<a.length;i++) {
			System.out.print(a[i] + " ") ;
		}
		System.out.println() ;
	}

	public static boolean isSorted(Comparable[] a){    //判断是否已排序;
		for(int i=1;i<a.length;i++){
			if (less(a[i],a[i-1])) {
				return false ;
			}
		}
		return true ;
	}

	public static void main(String[] args){//测试;

		System.out.println("please input the length of array:") ;

		Scanner sc = new Scanner(System.in) ;
		int len = sc.nextInt() ;
		String[] a = new String[len] ;

		System.out.println("please input the numbers of array:") ;
		for (int i=0;i<len;i++) {
			a[i] = sc.next() ;
		}

		if (!isSorted(a)) {
			insertionSort02(a) ;
		}

		show(a) ;
	}
}

  

4、希尔排序

import java.util.Scanner ;

public class Shell{                //希尔排序;
	public static void shellSort(Comparable[] a){
		int n = a.length ;
		int h = 1 ;
		while(h<n/3){
			h = 3*h + 1 ;
		}
		while(h>=1){
			for (int i=h;i<n;i++) {
				for (int j=i;j>=h && less(a[j],a[j-h]);j=j-h) {
					exch(a,j,j-h) ;
				}
			}
			h = h/3 ;
		}
	}

	private static boolean less(Comparable v, Comparable w){  //判断元素大小;
		if (v.compareTo(w)<0) {
			return true ;
		}else{
			return false ;
		}
	}

	private static void exch(Comparable[] a, int i, int j){    //交换元素;
		Comparable temp = a[i] ;
		a[i] = a[j] ;
		a[j] = temp ;
	}

	private static void show(Comparable[] a){	   //打印;
		for (int i=0;i<a.length;i++) {
			System.out.print(a[i] + " ") ;
		}
		System.out.println() ;
	}

	public static boolean isSorted(Comparable[] a){     //判断是否已排序;
		for(int i=1;i<a.length;i++){
			if (less(a[i],a[i-1])) {
				return false ;
			}
		}
		return true ;
	}

	public static void main(String[] args){  //测试;

		System.out.println("please input the length of array:") ;

		Scanner sc = new Scanner(System.in) ;
		int len = sc.nextInt() ;
		String[] a = new String[len] ;

		System.out.println("please input the numbers of array:") ;
		for (int i=0;i<len;i++) {
			a[i] = sc.next() ;
		}

		if (!isSorted(a)) {
			shellSort(a) ;
		}

		show(a) ;
	}
}

 

5、归并排序

import java.util.Scanner ;

public class Merge{//归并排序;

	private static Comparable[] aux ;

	public static void mergeSort(Comparable[] a){
		aux = new Comparable[a.length] ;
		mergeSort(a,0,a.length-1) ;
	}
	private static void mergeSort(Comparable[] a, int lo, int hi){
		if (lo<hi) {
			int mid = (lo+hi)/2 ;
			mergeSort(a,lo,mid) ;
			mergeSort(a,mid+1,hi) ;
			merge(a,lo,mid,hi) ;
		}else{
			return ;
		}
	}
	private static void merge(Comparable[] a, int lo, int mid, int hi){
		int i = lo ;
		int j = mid+1 ;
		for (int k=lo;k<=hi;k++) {
			aux[k] = a[k] ;
		}
		for (int k=lo;k<=hi;k++) {
			if (i>mid) {
				a[k] = aux[j] ;
				j++ ;
			}else if(j>hi){
				a[k] = aux[i] ;
				i++ ;
			}else if(less(aux[i],aux[j])){
				a[k] = aux[i] ;
				i++ ;
			}else{
				a[k] = aux[j] ;
				j++ ;
			}
		}
	}

	private static boolean less(Comparable v, Comparable w){  //判断元素大小;
		if (v.compareTo(w)<0) {
			return true ;
		}else{
			return false ;
		}
	}

	private static void exch(Comparable[] a, int i, int j){   //交换元素;
		Comparable temp = a[i] ;
		a[i] = a[j] ;
		a[j] = temp ;
	}

	private static void show(Comparable[] a){	//打印;
		for (int i=0;i<a.length;i++) {
			System.out.print(a[i] + " ") ;
		}
		System.out.println() ;
	}

	public static boolean isSorted(Comparable[] a){   //判断是否已排序;
		for(int i=1;i<a.length;i++){
			if (less(a[i],a[i-1])) {
				return false ;
			}
		}
		return true ;
	}

	public static void main(String[] args){//测试;

		System.out.println("please input the length of array:") ;

		Scanner sc = new Scanner(System.in) ;
		int len = sc.nextInt() ;
		String[] a = new String[len] ;

		System.out.println("please input the numbers of array:") ;
		for (int i=0;i<len;i++) {
			a[i] = sc.next() ;
		}

		if (!isSorted(a)) {
			mergeSort(a) ;
		}

		show(a) ;
	}
}

6、快速排序

import java.util.Scanner ;

public class Quick{       //快速排序;
	public static void quickSort(Comparable[] a){
		quickSort(a,0,a.length-1) ;
	}
	private static void quickSort(Comparable[] a, int lo, int hi){
		if (lo<hi) {
			int j = partition(a,lo,hi) ;
			quickSort(a,lo,j-1) ;
			quickSort(a,j+1,hi) ;
		}else{
			return ;
		}
	}
	private static int partition(Comparable[] a, int lo, int hi){
		int i = lo ;
		int j = hi ;
		Comparable temp = a[lo] ;
		while(i!=j){
			while(i<j && !less(a[j],temp)){
				j-- ;
			}
			while(i<j && !less(temp,a[i])){
				i++ ;
			}
			if (i!=j) {
				exch(a,i,j) ;
			}
		}
		exch(a,lo,i) ;
		return j ;
	}

	private static boolean less(Comparable v, Comparable w){   //判断元素大小;
		if (v.compareTo(w)<0) {
			return true ;
		}else{
			return false ;
		}
	}

	private static void exch(Comparable[] a, int i, int j){    //交换元素;
		Comparable temp = a[i] ;
		a[i] = a[j] ;
		a[j] = temp ;
	}

	private static void show(Comparable[] a){	//打印;
		for (int i=0;i<a.length;i++) {
			System.out.print(a[i] + " ") ;
		}
		System.out.println() ;
	}

	public static boolean isSorted(Comparable[] a){   //判断是否已排序;
		for(int i=1;i<a.length;i++){
			if (less(a[i],a[i-1])) {
				return false ;
			}
		}
		return true ;
	}

	public static void main(String[] args){//测试;

		System.out.println("please input the length of array:") ;

		Scanner sc = new Scanner(System.in) ;
		int len = sc.nextInt() ;
		String[] a = new String[len] ;

		System.out.println("please input the numbers of array:") ;
		for (int i=0;i<len;i++) {
			a[i] = sc.next() ;
		}

		if (!isSorted(a)) {
			quickSort(a) ;
		}

		show(a) ;
	}
}

  

  

 

  

  

时间: 2024-08-09 19:51:55

排序专题一的相关文章

[MOOC笔记]排序专题(数据结构)

1.冒泡排序: 思路:将相邻的逆序元素交换为顺序排列,直到整个序列有序,算法如下: /** * 冒泡排序-最初实现,时间复杂度O(n^2) * @param arr 待排序的数组 * @param lo 待排序区间的起始位置 * @param hi 待排序区间的结束位置 */ public static void bubbleSort(int[] arr, int lo, int hi) { //对序列进行规模n-1次扫描 for (int i = lo; i < hi - 1; i++) {

hiho拓扑排序专题 ——第四十八、四十七周

拓扑排序·一 分析: 此题就是求一个有向图中是否存在环. 如存在环则输出"Wrong", 若不存在环, 说明课程安排的合理,输出"Correct". 题中的提示说的已经十分清楚了. 总的来说就是: ① 找出入度为0的点(说明该点没有前驱),把该点放入集合T中. 把所有从该点出发的边都删除: ② 遍历剩余的点, 找出入度为0 的点, 重复①操作. ③直到不存在入度为0的点. 结束.如果此时集合T中包含所有的点, 那么该图不存在环, 否则存在环. 注意:1.执行操作①时

三大基本排序专题

//以下依次是冒泡.选择.插入排序 var n,i:longint;    a:array[0..20] of longint;procedure BUB;var i,j,t:longint;begin    for i:=1 to n-1 do        for j:=1 to n-i do            if a[j]>a[j+1] then begin t:=a[j]; a[j]:=a[j+1]; a[j+1]:=t; end;end;procedure SEL;var i,j

括号匹配问题 -算法专题

算法数据结构面试分享 符号匹配问题 今天在帖子上看见有同学在问,如果一个字符串中包含大括号和小括号,我们该如何解决括号匹配问题.我们今天就一起看下这道题吧.按照我们之前的套路,按部就班来: 确保我们理解了问题,并且尝试一个例子,确认理解无误. 举个例子,这样的括号是匹配的, ().{}.({}), ({()}(){}), 而类似于{(.{,({)都是不匹配的. 想想你可以用什么方法解决问题,你会选择哪一种,为什么? 我们拿这个字符串为例,({()}(){}), 最后一个)匹配的是第一个(, 倒数

计数排序 - 算法数据结构面试分享(五)

数组排序问题 - 计数排序 昨天我们留了一道题目"给你一个整型数组,里面出现的数在[0-100] 之间,能用最优化的方法帮我排序吗". 1. 确保我们理解了问题,并且尝试一个例子,确认理解无误. 这是一道排序算法题,我们学过很多排序的算法.不一样的是,它给定一个额外的条件,数组里的每个数字都在1-100之间.如果我们采取传统的排序算法,这个条件我们好像用不上.大家在面试的时候如果发现有条件没有用上,基本上我们给出的算法可能不是最优的,或者我们没有解决它最原始的需求.举个例子{50, 4

算法数据结构面试分享(六)数组排序问题(2) - 计数排序

数组排序问题(2) 昨天我们留了一道题目"给你一个整型数组,里面出现的数在[0-100] 之间,能用最优化的方法帮我排序吗". 1. 确保我们理解了问题,并且尝试一个例子,确认理解无误. 这是一道排序算法题,我们学过很多排序的算法.不一样的是,它给定一个额外的条件,数组里的每个数字都在1-100之间.如果我们采取传统的排序算法,这个条件我们好像用不上.大家在面试的时候如果发现有条件没有用上,基本上我们给出的算法可能不是最优的,或者我们没有解决它最原始的需求.举个例子{50, 46, 5

HDU2647 topsort

Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to  distribute rewards to his workers. Now he has a trouble about how to distribute the rewards.   The workers will compare their rewards ,and s

一步一步 copy163: 网易严选 ---- vue-cli

参考 网易严选商城小程序全栈开发,域名备案中近期上线(mpvue+koa2+mysql) 小程序服务端源码地址 小程序源码地址 功能实现 api :                   ------------ 致谢 heyushuo 点这儿给个 star 吧~ 首页  https://www.heyuhsuo.xyz/heyushuo/index/index /pages/index/index.vue 专题  https://www.heyuhsuo.xyz/heyushuo/topic/l

【C】【第十一课】

[_(:з)∠)_咸_(:з)∠)_鱼_(:з)∠)_本_(:з)∠)_鱼_(:з)∠)_]   [知识梳理] [典例分析] [第六十一题] 题目:打印出杨辉三角形(要求打印出10行). 思路:构造10*19的二维数值数组,边界值等于中间值等于上一行的前位值+左位值.再将非零元素打出. 1 #include <stdio.h> 2 3 int main() 4 { 5 //数值矩阵 6 char a[10][19]; 7 int i = 0; 8 int j = 0; 9 //初始化 10 f