十大经典排序算法java(几种排序算法的比较)

四种常用排序算法冒泡排序特点:效率低,实现简单思想(从小到大排):每一趟将待排序序列中最大元素移到最后,剩下的为新的待排序序列,重复上述步骤直到排完所有元素。这只是冒泡排序的一种,当然也可以从后往前排。publicvoidbubbleSort(intarray[]){intt=0;for(inti=0;i<…

大家好,又见面了,我是你们的朋友全栈君。

四种常用排序算法

注:从小到大排

冒泡排序

特点:效率低,实现简单
思想:每一趟将待排序序列中最大元素移到最后,剩下的为新的待排序序列,重复上述步骤直到排完所有元素。这只是冒泡排序的一种,当然也可以从后往前排。

public void bubbleSort(int array[]) { 
   
		int t = 0;
		for (int i = 0; i < array.length - 1; i++)
			for (int j = 0; j < array.length - 1 - i; j++)
				if (array[j] > array[j + 1]) { 
   
					t = array[j];
					array[j] = array[j + 1];
					array[j + 1] = t;
				}
	}

选择排序

特点:效率低,容易实现。
思想:每一趟从待排序序列选择一个最小的元素放到已排好序序列的末尾,剩下的为待排序序列,重复上述步骤直到完成排序。

	public void selectSort(int array[]) { 
   
		int t = 0;
		for (int i = 0; i < array.length - 1; i++){ 
   
			int index=i;
			for (int j = i + 1; j < array.length; j++)
				if (array[index] > array[j])
					index=j;
			if(index!=i){ 
    //找到了比array[i]小的则与array[i]交换位置
				t = array[i];
				array[i] = array[index];
				array[index] = t;
			}
		}
	}

插入排序

特点:效率低,容易实现。
思想:将数组分为两部分,将后部分元素逐一与前部分元素比较,如果前部分元素比array[i]小,就将前部分元素往后移动。当没有比array[i]小的元素,即是合理位置,在此位置插入array[i]

public void insertionSort(int array[]) { 
   
		int i, j, t = 0;
		for (i = 1; i < array.length; i++) { 
   
			if(a[i]<a[i-1]){ 
   
				t = array[i];
				for (j = i - 1; j >= 0 && t < array[j]; j--)
					array[j + 1] = array[j];
				//插入array[i]
				array[j + 1] = t;
			}
		}
}

快速排序

特点:高效,时间复杂度为nlogn。
采用分治法的思想:首先设置一个轴值pivot,然后以这个轴值为划分基准将待排序序列分成比pivot大和比pivot小的两部分,接下来对划分完的子序列进行快排直到子序列为一个元素为止。

public void quickSort(int array[], int low, int high) { 
   // 传入low=0,high=array.length-1;
		int pivot, p_pos, i, t;// pivot->位索引;p_pos->轴值。
		if (low < high) { 
   
			p_pos = low;
			pivot = array[p_pos];
			for (i = low + 1; i <= high; i++)
				if (array[i] > pivot) { 
   
					p_pos++;
					t = array[p_pos];
					array[p_pos] = array[i];
					array[i] = t;
				}
			t = array[low];
			array[low] = array[p_pos];
			array[p_pos] = t;
			// 分而治之
			quickSort(array, low, p_pos - 1);// 排序左半部分
			quickSort(array, p_pos + 1, high);// 排序右半部分
		}
}

测试demo:

import java.util.Arrays;
public class sortTest { 
   
	// 冒泡排序
	public void bubbleSort(int array[]) { 
   
		int t = 0;
		for (int i = 0; i < array.length - 1; i++)
			for (int j = 0; j < array.length - 1 - i; j++)
				if (array[j] > array[j + 1]) { 
   
					t = array[j];
					array[j] = array[j + 1];
					array[j + 1] = t;
				}
	}

	// 选择排序
	public void selectSort(int array[]) { 
   
		int t = 0;
		for (int i = 0; i < array.length - 1; i++){ 
   
			int index=i;
			for (int j = i + 1; j < array.length; j++)
				if (array[index] > array[j])
					index=j;
			if(index!=i){ 
    //找到了比array[i]小的则与array[i]交换位置
				t = array[i];
				array[i] = array[index];
				array[index] = t;
			}
		}
	}

	public void insertionSort(int array[]) { 
   
			int i, j, t = 0;
			for (i = 1; i < array.length; i++) { 
   
				if(a[i]<a[i-1]){ 
   
					t = array[i];
					for (j = i - 1; j >= 0 && t < array[j]; j--)
						array[j + 1] = array[j];
					//插入array[i]
					array[j + 1] = t;
				}
			}
	}

	// 分治法快速排序
	public void quickSort(int array[], int low, int high) { 
   // 传入low=0,high=array.length-1;
		int pivot, p_pos, i, t;// pivot->位索引;p_pos->轴值。
		if (low < high) { 
   
			p_pos = low;
			pivot = array[p_pos];
			for (i = low + 1; i <= high; i++)
				if (array[i] > pivot) { 
   
					p_pos++;
					t = array[p_pos];
					array[p_pos] = array[i];
					array[i] = t;
				}
			t = array[low];
			array[low] = array[p_pos];
			array[p_pos] = t;
			// 分而治之
			quickSort(array, low, p_pos - 1);// 排序左半部分
			quickSort(array, p_pos + 1, high);// 排序右半部分
		}
	}

	public static void main(String[] args) { 
   
		// TODO Auto-generated method stub
		int[] array = { 
    37, 47, 23, 100, 19, 56, 56, 99, 9 };
		sortTest st = new sortTest();
		// st.bubbleSort(array);
		// st.selectSort(array);
		// st.insertionSort(array);
		st.quickSort(array, 0, array.length - 1);
		System.out.println("排序后:" + Arrays.toString(array));
	}
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/128175.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • smtp搭建_smtp服务器指的是什么服务器

    smtp搭建_smtp服务器指的是什么服务器应用目标:更稳定地发送邮件实现难度:★★☆☆☆我们在发送电子邮件的时候,这封E-mail首先来到ISP提供的邮件服务器,再通过它发送出去。但如果ISP因为网络出现一些问题,则可能会耽搁邮件的发送,甚至可能会造成邮件丢失。如果用自己的机器做SMTP服务器来发邮件,那肯定不会出现上述情况啦!怎么样,心动了吧?下面咱们就一起来架设一个属于自己的SMTP服务器,让你的E-mail发送更安全。一、SMTP服…

    2022年10月3日
    7
  • 大数据ETL详解

    大数据ETL详解

    2021年10月28日
    41
  • 编译安装httpd apache服务器

    编译安装httpd apache服务器

    2022年4月2日
    37
  • ▲ Android 自定义日历签到效果

    ▲ Android 自定义日历签到效果

    2021年3月12日
    180
  • vim撤销、回退操作「建议收藏」

    vim撤销、回退操作「建议收藏」打个广告,请有意向加入腾讯的前端,将简历发送至mzxbupt@gmail.com在vi中按u可以撤销一次操作u  撤销上一步的操作Ctrl+r恢复上一步被撤销的操作注意:如果你输入“u”两次,你的文本恢复原样,那应该是你的Vim被配置在Vi兼容模式了。重做如果你撤销得太多,你可以输入CTRL-R(redo)回退前一个命令。换句话说,它撤销一个撤销。要看执行的…

    2022年6月16日
    409
  • mysql只有information_schema_validationquery not set

    mysql只有information_schema_validationquery not set在MySQL8.0以前,通常会通过infomation_schema的表来获取一些元数据,例如从tables表中获取表的下一个auto_increment值,从indexes表获取索引的相关信息等。但在MySQL8.0去查询这些信息的时候,出现了不准确的情况。例如auto_increment,–此时test表的auto_increment是204mysql&amp;gt;showcreate…

    2022年9月16日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号