列出三个最简单的排序方法:
1,冒泡排序;2,选择排序;3,插入排序
这里的排序针对都是简单类型的数组进行排序,他们的比较次数都为O(n^2),但是相对来说,插入排序的交换次数是最少的,大概为冒泡排序的一半,其选择排序所用的交换次数也要少。这三个排序都是稳定的排序算法。
1,冒泡排序:
执行该程序,对于1000000个元素的数组排序,其算法所需时间大概为212ms
package pku.ss.datastructure.Sort;
public class BubbleSort {
public static void main(String[] args) {
int max = 1000000;
ArrayBubble arr = new ArrayBubble(max);
long startTime = System.currentTimeMillis();
for (int j = 0; j < max; j++) {
double element = (double) (java.lang.Math.random() * (max - 1));
arr.insert(element);
}
long endTime = System.currentTimeMillis();
System.out.println("Sort time: " + (endTime - startTime) + " ms");
}
}
class ArrayBubble {
private int nElement;
private double[] array;
ArrayBubble(int max) {
array = new double[max];
nElement = 0;
}
public void insert(double x) {
// if (array.length == 10) {
// System.out.println("[ERROR]Out of memory!");
// System.out.println("[INFO]Atempt to insert into a full array");
// System.exit(0);
// } else {
array[nElement] = x;
nElement++;
// }
}
public void display() {
for (int i = 0; i < nElement; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
System.out.println();
}
public void bubbleSort() {
int outer;
for (outer = nElement - 1; outer > 0; outer--)
for (int j = 0; j < outer; j++) {
if (array[j] > array[j + 1]) {
swap(j, j + 1);
}
}
}
private void swap(int x, int y) {
double temp;
temp = array[x];
array[x] = array[y];
array[y] = temp;
}
}
2,选择排序
执行该程序,对于1000000个元素的数组排序,其算法所需时间大概为216ms
package pku.ss.datastructure.Sort;
public class SelectSort {
public static void main(String[] args) {
int max = 1000000;
ArraySel arr = new ArraySel(max);
long startTime = System.currentTimeMillis();
for (int j = 0; j < max; j++) {
double element = (double) (java.lang.Math.random() * (max - 1));
arr.insert(element);
}
long endTime = System.currentTimeMillis();
System.out.println("Sort time: " + (endTime - startTime) + " ms");
}
}
/** ***************************************** */
class ArraySel {
private double[] a;
private int nElement;
//--------------------------------------------------------
public ArraySel(int max) {
a = new double[max];
nElement = 0;
}
//--------------------------------------------------------
public void insert(double element) {
a[nElement] = element;
nElement++;
}
//--------------------------------------------------------
public void display() {
for (int i = 0; i < nElement; i++)
System.out.print(a[i] + " ");
System.out.println();
}
//--------------------------------------------------------
public void selectSort() {
int out, in, min;
for (out = 0; out < nElement - 1; out++) {
min = out;
for (in = out + 1; in < nElement; in++) {
if (a[in] < a[min]) {
min = in;
}
}
if(min!=out)
swap(min, out);
}
}
//--------------------------------------------------------
private void swap(int x, int y) {
double temp;
temp = a[x];
a[x] = a[y];
a[y] = temp;
}
}
3,插入排序
执行该程序,对于1000000个元素的数组排序,其算法所需时间大概为214ms
package pku.ss.datastructure.Sort;
public class InsertionSort {
public static void main(String[] args) {
int max = 1000000;
ArrayIns arr = new ArrayIns(max);
long startTime = System.currentTimeMillis();
for (int j = 0; j < max; j++) {
double element = (double) (java.lang.Math.random() * (max - 1));
arr.insert(element);
}
long endTime = System.currentTimeMillis();
System.out.println("Sort time: " + (endTime - startTime) + " ms");
}
}
class ArrayIns {
private double[] a;
private int nElement;
// --------------------------------------------------------
public ArrayIns(int max) {
a = new double[max];
nElement = 0;
}
// --------------------------------------------------------
public void insert(double element) {
a[nElement] = element;
nElement++;
}
// --------------------------------------------------------
public void display() {
for (int i = 0; i < nElement; i++)
System.out.print(a[i] + " ");
System.out.println();
}
// --------------------------------------------------------
public void insertionSort() {
int out, in;
for (out = 1; out < nElement; out++) {
double temp = a[out];
in = out;
while (in > 0 && a[in - 1] >= temp) {
a[in] = a[in - 1];
in--;
}
a[in] = temp;
}
}
// --------------------------------------------------------
}
分享到:
- 2008-05-05 11:09
- 浏览 1792
- 评论(1)
- 论坛回复 / 浏览 (1 / 2268)
- 查看更多
相关推荐
这本书是用java讲数据结构和排序算法的,全书只有44页,但是讲的内容很不错,推荐学习或者复习数据结构的童鞋下在看看!
java数据结构的大作业,各种排序算法的性能比较。
Java数据结构的作业,写出直接插入排序、希尔排序、冒泡排序、快速排序、选择排序、归并排序的算法,并用动态界面展示出来。
详细讲述了8中常见算法的原理及思想,并用JAVA进行了实现,代码中有详细的注释,解释了算法的实现逻辑和一些小技巧。
java数据结构用到的排序程序
总结的不错,值得一看 ...插入排序(直接插入排序、折半插入排序、希尔排序); * 2.交换排序(冒泡泡排序、快速排序); * 3.选择排序(直接选择排序、堆排序); * 4.归并排序; * 5.基数排序。
数据结构之排序实验报告 数据结构之排序实验报告
数据结构之选择排序
《Java数据结构和算法》(第2版)介绍了计算机编程中使用的数据结构和算法,对于在计算机应用中如何操作和管理数据以取得最优性能提供了深入浅出的讲解。全书共分为15章,分别讲述了基本概念、数组、简单排序、堆和...
java基础笔记数据结构-排序二叉树,详细描述了排序二叉树的原理及其实现方式,基础数据结构。
数据结构之JAVA排序JAVASORT.pdf
数据结构之冒泡排序
数据结构的Java实现源代码Java实现数据结构上的堆栈树图,排序查找
JAVA数据结构与算法课程第05课双端链表和双向链表.mp4JAVA数据结构与算法课程第06课递归的应用.mp4JAVA数据结构与算法课程第07课递归的高级应用.mp4JAVA数据结构与算法课程第08课希尔排序.mp4JAVA数据结构与算法课程...
java 数据结构和算法, 排序算法, 数组,链表,二叉树
数据结构堆排序的java算法实现,里面用java语言实现了堆排序的算法实现,有输入和输出结果
数据结构 二叉排序树 二叉搜索树 java swing图形界面实现
此文档是作者收集整理的数据结构中常见的排序算法,包括算法实现的图解及Java代码,如有错误的地方还望指出,大家相互学习。
包含各种典型内排序的java算法,包括: 冒泡排序,堆排序,插入排序,合并排序,快速排序,Shell排序(代码未完成),直接选择排序。 各种排序效率对比参见: ...
基于java数据结构实验快速排序,归并排序,堆排).pdf