public class SortMain {
// 用于临时储值
static int temp = 0;
public static void main(String[] args) {
// 需要排序的数据
int[] Source = { 2, 4, 6, 1, 3, 5, 8, 4, 10, 18 };
// 冒泡排序法
// printSource(bubbleSort(Source));
// 插入排序法
// printSource(insertSort(Source));
// 反转数组
// printSource(reverseSort(Source));
// 希尔排序法(shell)
// printSource(shellSort(Source));
// 选择排序法
// printSource(selectSort(Source));
//printSource(Source);
// 快速排序法
//printSource(QuickSort(Source, 0, Source.length - 1));
}
// 使用冒泡排序法
public static int[] bubbleSort(int[] Source) {
// 获取需要排序的数据
int[] bubbleArray = Source;
// 进行排序
for (int i = 0; i < bubbleArray.length; i++)// 从数组第一个元素进行遍历
{
for (int j = i + 1; j < bubbleArray.length; j++)// 从下标为i+1的元素进行遍历
{
if (bubbleArray[i] > bubbleArray[j])// 对bubbleArray[i]>bubbleArray[j]进行比较
{
temp = bubbleArray[i]; // 将bubbleArray[i]存于临时变量中
bubbleArray[i] = bubbleArray[j]; // 实现i和j的数据对换操作
bubbleArray[j] = temp;
}
}
}
return bubbleArray;
}
/***
*
* @param Source 返回数组
* @return
*
* 插入排序法 Insertion Sort
* 简言之,插入排序就是每一步都讲一个待排数据按其大小插入到已经排序的数据中的
* 适当位置,知道全部插入完毕。插入排序方法分直接插入排序和折半插入排序两种,
* 这里只介绍直接插入排序。
*/
// 使用插入排序法
public static int[] insertSort(int[] Source) {
// 获取需要排序的数据
int[] insertSort = Source;
// 开始执行插入排序
for (int i = 1; i < insertSort.length; i++) {
for (int j = 0; j < i; j++) {
if (insertSort[j] > insertSort[i]) {
temp = insertSort[i];
insertSort[i] = insertSort[j];
insertSort[j] = temp;
}
}
}
return insertSort;
}
// 反转数组法
public static int[] reverseSort(int[] Source) {
// 获取需要排序的数据
int[] reverseSort = Source;
for (int i = 0; i < reverseSort.length / 2; i++) {
temp = reverseSort[i];
reverseSort[i] = reverseSort[reverseSort.length - i - 1];
reverseSort[reverseSort.length - 1 - i] = temp;
}
return reverseSort;
}
// 希尔排序法
public static int[] shellSort(int[] Source) {
for (int increment = Source.length / 2; increment >= 2; increment /= 2) {
// System.out.print(increment+"\t");
System.out.println();
for (int i = 0; i < increment; i++) {
insertSource(Source, i, increment);
}
}
System.out.println();
// System.out.println("执行最后一步");
printSource(Source);
// System.out.println("执行完毕");
insertSource(Source, 0, 1);
// System.out.println();
return Source;
}
// 希尔排序法中的调用方法
public static void insertSource(int[] Source, int start, int inc) {
System.out.print("start:" + start + "inc:" + inc + "\t");
System.out.println();
int temp;
for (int i = start + inc; i < Source.length; i += inc) {
System.out.println("i-------:" + i);
for (int j = i; j >= inc; j -= inc) {
System.out.println("j:" + j);
if (Source[j] < Source[j - inc]) {
temp = Source[j];
Source[j] = Source[j - inc];
Source[j - inc] = temp;
}
}
}
}
/***
*
* @param Source
* @return
* 选择排序法 Selecttion Sort
* 选择排序的基本思想是:对待排序的记录序列进行n-1遍的处理,第一遍是将L[1..n]中
* 的最小者与L[1]交换位置,第二遍处理是讲L[2..n]中的最小者与L[2]交换位置,....,第i遍
* 处理是讲L[i..n]中的最小者与L[i]交换位置。这样,经过i遍处理之后,前i个记录的位置就已
* 经从小到大的顺序排列好了。
*
* 选择排序于冒泡排序的区别:冒泡排序每次比较后,如果发现书序不对立即进行交换,而选择排序
* 不立即进行交换,而是找出最小的元素后在进行交换
*/
// 选择排序法
public static int[] selectSort(int[] Source) {
// 获取需要排序的数据
int[] selectSort = Source;
int index;
for (int i = 0; i < selectSort.length; i++) {
index = i;
for (int j = i + 1; j < selectSort.length; j++) {
if (selectSort[index] > selectSort[j]) {
index = j;
}
}
temp = selectSort[i];
selectSort[i] = selectSort[index];
selectSort[index] = temp;
}
return selectSort;
}
/***
* 快速排序法
* 快速排序是对冒泡排序的一种改进,它的基本思想是:通过一趟排序将排序的数据分割
* 成独立的两部分,其中一部分的所有数据都比另一部分的所有数据都要小,然后再按此
* 方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数
* 据变成有序序列
*/
// 快速排序法
static boolean bool = true;
public static int[] QuickSort(int[] Source, int left, int right) {
int i = left;
int j = right;
int temp = 0;
int between = Source[(left + right) / 2];
do {
while (Source[i] < between && i <= right) {
i++;
}
while (Source[j] > between && j >= left) {
j--;
}
if (i <= j) {
temp = Source[i];
Source[i] = Source[j];
Source[j] = temp;
i++;
j--;
}
/*if (bool) {
printSource(Source);
bool = false;
}*/
} while (i <= j);
printSource(Source);
if (left < j) {
QuickSort(Source, left, j);
}
if (right > i) {
QuickSort(Source, i, right);
}
return Source;
}
// 打印方法
public static void printSource(int[] Source) {
for (int i = 0; i < Source.length; i++) {
System.out.print(Source[i] + "\t");
}
System.out.println();
}
}
分享到:
相关推荐
java排序方法 java排序方法 java排序方法 java排序方法
Java排序方法详解大全 Java排序 快速排序 冒泡排序
java排序方法.pdf
初学Java中有对一串数字的排序,而且对考试面试什么的都有可能用得到,帮助学习,增加知识有冒泡排序选择排序等等几种方式
java实现的常用的几种基本排序算法,插入、交换、选择、归并
简单的8种排序方法,有冒泡排序,基本排序,快速排序等等等等
java的一些常用的排序方法收集,希望对各位有所帮助。
提供了Java实现几种常见排序方法和原理介绍
java的一些常见排序方法分析,冒泡排序,插入排序等,多种排序方法思路的分析。
使用java实现的选择、冒泡、插入、快速、希尔排序以及折半查找
java 冒泡排序方法,java 冒泡排序方法。
附件为以上方法必须用到的包,有朋友问到,现提供下载。 博文链接:https://ReturnOfKing.iteye.com/blog/248274
java 排序方法 面试的时候会碰到 下载学习吧
JAVA排序汇总JAVA排序汇总JAVA排序汇总
java排序 java 排序 排序大全 算法 java算法
java排序.txt
主要为大家详细介绍了Java排序方法sort用法,对数组、集合的排序方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
java排序,简单介绍了java的主要有那些排序方法。
java排序算法使用及场景说明 文档后面有一些别人的链接,多在google上搜索Java排序算法,及维基百科上面也有很全的算法介绍。
本文实例讲述了Java编程实现中英混合字符串数组按首字母排序的方法。分享给大家供大家参考,具体如下: 在Java中对于字符串数组的排序,我们可以使用Arrays.sort(String[])方法很便捷的进行排序。例如: String[]...