一、 上机目的
要求学生掌握:
1、进一步熟悉线性表数据结构的定义
2、实现线性表基础上的各种排序方法和算法(简单和部分先进排序方法)
3、将排序算法应用于一些实际问题。
二、 上机内容
1、设一个线性表,表内存放8个随机数字,分别使用选择、冒泡以及快速排序算法,对这个线性表进行排序。
2、从键盘输入或从文件中读取有限个任意同类型的数据生成无序线性表,并用简单排序方法和快速、堆排序方法、希尔排序方法对其进行排序,排序后,再从键盘输入一个同类型的数据,插入后使线性表仍然有序。
3*、奇偶交换排序如下所述:第一趟对所有奇数i ,将a[i]和a[i+1]进行比较;第二趟对所有的偶数i , 将a[i]和a[i+1]进行比较,若a[i]>a[i+1],则将两者交换;第三趟对奇数i ;第四趟对偶数i …依次类推,直至整个序列有序为止。试问这种排序方法的结束条件是什么?编写实现上述奇偶交换排序的算法。
代码实现
1、设一个线性表,表内存放8个随机数字,分别使用选择、冒泡以及快速排序算法,对这个线性表进行排序。
package com.上机.ch06;
public class Selection {
/**
* 每次遍历,选择出最小的
* @param a
*/
public static void SelectionSort(Comparable[] a){
for (int i = 0; i < a.length -1; i++) {
int minIndex = i;
for (int j = i + 1; j < a.length; j++) {
if (greater(a[minIndex],a[j])) minIndex = j;
}
wrap(a,minIndex,i);
}
}
public static boolean greater(Comparable i,Comparable j){
return i.compareTo(j) > 0;
}
public static void wrap(Comparable[] a,int i,int j){
Comparable temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
package com.上机.ch06;
public class Bubble {
public static void BubbleSort(Comparable[] a){
for(int i = a.length - 1;i > 0;i--){
for(int j = 0;j < i;j++){
if(greater(a[j],a[j+1])) wrap(a,j,j+1);
}
}
}
/**
* 比较元素v是否大于元素w
* @param v
* @param w
* @return
*/
public static boolean greater(Comparable v,Comparable w){
return v.compareTo(w) > 0;
}
/**
* 数组元素i和j换位置
* @param a
* @param i
* @param j
*/
public static void wrap(Comparable[] a,int i,int j){
Comparable temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
package com.上机.ch06;
public class Quick {
/**
* 对数组内元素进行排序
* @param a
*/
public static void quickSort(Comparable[] a){
int lo = 0;
int hi = a.length -1;
quickSort(a,lo,hi);
}
public static void quickSort(Comparable[] a,int l,int r){
if( l >= r) return;
int mid = (l + r) / 2;
int i = l - 1,j = r + 1;
while(i < j){ // 循环结束 i==j
do{
i++;
}while (greater(a[mid],a[i]));
do{
j--;
}while(greater(a[j],a[mid]));
if(i < j) wrap(a,i,j);
}
quickSort(a,l,j); // 递归处理左边
quickSort(a,j+1,r); // 递归处理右边
}
public static boolean greater(Comparable i,Comparable j){
return i.compareTo(j) > 0;
}
public static void wrap(Comparable[] a,int i,int j){
Comparable temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
2、从键盘输入或从文件中读取有限个任意同类型的数据生成无序线性表,并用简单排序方法和快速、堆排序方法、希尔排序方法对其进行排序,排序后,再从键盘输入一个同类型的数据,插入后使线性表仍然有序。
package com.上机.ch06;
public class Heap<T extends Comparable<T>>{
private T[] items;
private int length;
public Heap(int capacity) {
this.length = 0;
items = (T[])new Comparable[capacity + 1];
}
/**
* 比较两个节点的大小
* @param i
* @param j
* @return
*/
private boolean less(int i,int j){
return items[i].compareTo(items[j]) < 0;
}
private void swap(int i,int j){
T temp = items[i];
items[i] = items[j];
items[j] = temp;
}
public void insert(T t){
items[++length] = t;
swim(length);
}
// 上浮算法
private void swim(int k){
while( k > 1){
if (less(k/2,k)) swap(k,k/2);
k /= 2;
}
}
public T deleteMax(){
T max = items[1];
swap(1,length);
items[length] = null;
length--;
sink(1);
return max;
}
private void sink(int k){
while(k*2 <= length){
int max;
if(k*2+1 <= length){
if (less(k*2,k*2+1)) max = k*2+1;
else max = k*2;
}else{
max = k*2;
}
if (less(k,max)) swap(k,max);
else return;
}
}
}
package com.上机.ch06;
import java.util.Arrays;
import java.util.Random;
public class HeapSort {
/**
* 对source数组中的数据从小到大排列
*
* @param source
*/
public static void sort(Comparable[] source) {
Comparable[] heap = new Comparable[source.length + 1];
createHeap(source, heap);
int N = heap.length - 1;
while (N != 1) {
wrap(heap, 1, N);
N--;
sink(heap, 1, N);
}
System.arraycopy(heap, 1, source, 0, source.length);
}
private static void createHeap(Comparable[] source, Comparable[] heap) {
System.arraycopy(source, 0, heap, 1, source.length);
// 对堆中元素做下沉处理
for (int i = (heap.length - 1) / 2; i > 0; i--) {
sink(heap, i, heap.length - 1);
}
}
/**
* 判断heap堆中索引i处的元素是否小于索引j处的元素
*
* @param heap
* @param i
* @param j
* @return
*/
private static boolean less(Comparable[] heap, int i, int j) {
return heap[i].compareTo(heap[j]) < 0;
}
/**
* 交换heap堆中i索引和j索引处的值
*
* @param heap
* @param i
* @param j
*/
private static void wrap(Comparable[] heap, int i, int j) {
Comparable temp = heap[i];
heap[i] = heap[j];
heap[j] = temp;
}
/**
* 在heap堆中,对target处元素做下沉,范围是0~range
*
* @param heap
* @param target
* @param range
*/
private static void sink(Comparable[] heap, int target, int range) {
while (2 * target <= range) {
int max;
if (2 * target + 1 <= range) {
if (less(heap, 2 * target, 2 * target + 1)) max = 2 * target + 1;
else max = 2 * target;
} else {
max = 2 * target;
}
if (less(heap, target, max)) wrap(heap, max, target);
target = max;
}
}
public static Integer[] randomArray(int N) {
Random random = new Random();
Integer[] array = new Integer[N];
for(int i = 0;i < N;i++){
array[i] = random.nextInt(100);
}
return array;
}
public static void main(String[] args) {
Integer[] array = randomArray(10);
sort(array);
System.out.println(Arrays.toString(array));
}
}
package com.上机.ch06;
import java.util.Arrays;
public class Shell<T> {
public static void shellSort(Comparable[] a){
// 1.根据数组a的长度,确定增长量h的初始值;
int h = 1;
while(h < a.length/2){
h = 2* h +1;
}
while(h >= 1){
for (int i = h;i < a.length;i++){
for (int j = i; j >= h; j-=h) {
if (greater(a[j-h],a[j])) swap(a,j,j-h);
}
}
h /= 2;
}
}
public static void swap(Comparable[] a,int i,int j){
Comparable temp = a[i];
a[i] = a[j];
a[j] = temp;
}
public static boolean greater(Comparable v,Comparable w){
return v.compareTo(w) > 0;
}
public static void main(String[] args) {
Integer[] a = {4, 6, 8, 2, 9, 10, 3};
shellSort(a);
System.out.println(Arrays.toString(a));
}
}
package com.上机.ch06;
public class Quick {
/**
* 对数组内元素进行排序
* @param a
*/
public static void quickSort(Comparable[] a){
int lo = 0;
int hi = a.length -1;
quickSort(a,lo,hi);
}
public static void quickSort(Comparable[] a,int l,int r){
if( l >= r) return;
int mid = (l + r) / 2;
int i = l - 1,j = r + 1;
while(i < j){ // 循环结束 i==j
do{
i++;
}while (greater(a[mid],a[i]));
do{
j--;
}while(greater(a[j],a[mid]));
if(i < j) wrap(a,i,j);
}
quickSort(a,l,j); // 递归处理左边
quickSort(a,j+1,r); // 递归处理右边
}
public static boolean greater(Comparable i,Comparable j){
return i.compareTo(j) > 0;
}
public static void wrap(Comparable[] a,int i,int j){
Comparable temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}