public class List {
int[] element; //数组
int listLength; //数组当前元素个数
int listSize; //数组空间大小
//构造函数
public List(int listSize) {
element = new int[liseSize > 10 ? liseSize : 10]; //数组大小最小是10
this.listSize = listSize;
listLength = 0;
}
//多态构造函数,缺省情况下数组大小为10
public List() {
this(10);
}
//在第i个位置插入元素 e
public int insertList(int index, int e) throws Exception {
if (index < 0 || index > this.listLength) {
throw new Exception("第" + index + "元素超出范围!");
}
//如果数组已经满了 重新新建+10大小的数组
if (listLength >= listSize){
int[] newElement = new int[listSize + 10];
for (int i = 0; i < this.listLength; i++) {
newElement[i] = this.element[i];
}
this.element = newElement;
listSize += 10;
}
//插入新元素
for (int i = listLength; i > index; i--) //index之后的元素都后移一位
{
this.element[i] = this.element[i - 1];
}
this.element[index] = e;
listLength++; //当前长度加1
return e;
}
//检查线性表是否为空
public boolean isEmptyList() {
return listLength == 0;
}
//清空线性表
public boolean clearList() {
listLength = 0;
int[] newElement = new int[10]; //重新初始化数组大小为10
this.element = newElement;
return true;
}
//删除第i个元素
public int deleteList(int index) throws Exception {
if (index < 0 || index >= listLength) {
throw new Exception("第" + index + "元素超出范围!");
}
int e = element[index];
for (int i = index; i < listLength; i++) { //index之后所有元素前移1位
element[i] = element[i + 1];
}
listLength--;
return e;
}
//遍历打印数组
public void printList() {
if (this.isEmptyList())
System.out.println("数组为空!\n");
else
System.out.println("开始遍历数组:\n");
for (int i = 0; i < listLength; i++) {
System.out.print(element[i] + "\t");
}
System.out.println("\n");
}
//查找字符e出现的第一个位置
public int searchList(int e) {
for (int i = 0; i < listLength; i++)
if (element[i] == e)
return i;
System.out.println("数组中没有" + e + "元素");
return -1;
}
/*
* 快速排序 左边作参考值 挖坑填坑法
* 快速排序中间作参考值
* 冒泡排序法
* 双向冒泡排序法
* 堆排序
* 插入排序
* 希尔排序
* 折半排序
*/
//快速排序 最低位作参考值 挖坑填坑法
public boolean quickSortListLeft(int left, int right) {
if (left >= right)
return true;
int i, j, temp;
i = left;
j = right;
temp = element[left]; //i流出一个坑
while (i < j) {
while (element[j] > temp && i < j)
j--;
if (i < j)
element[i++] = element[j];//填了i的坑,j留出一个坑
while (element[i] < temp && i < j)
i++;
if (i < j)
element[j--] = element[i]; //填j的坑,留了i的坑
} //i=j,此时此处留了一个坑给temp
element[i] = temp;
//element[j] = temp; //等价
quickSortListLeft(left, i - 1); //左右递归
quickSortListLeft(j + 1, right);
return true;
}
//快速排序中间作参考值
public boolean quickSortList(int left, int right) {
this.printList();
if (left >= right)
return true;
int i, j, temp, key;
i = left;
j = right;
temp = element[(i + j) / 2];
while (i < j) {
while (element[i] < temp && i < j) {
i++;
}
while (element[j] > temp && i < j) {
j--;
}
//i、j走到位置后交换a[i]<-->a[j]
key = element[i];
element[i] = element[j];
element[j] = key;
i++;
j--;
}
quickSortList(left, j);
quickSortList(i, right);
return true;
}
//冒泡排序法
public boolean bubbleSortList()
{
boolean status = true;
int temp;
for (int i = listLength - 1; i > 0 && status; i--) {
status = false;
for (int j = 0; j < i; j++) {
if (element[j] > element[j + 1]) {
temp = element[j];
element[j] = element[j+1];
element[j+1] = temp;
status = true;
}
}
}
return true;
}
//双向冒泡排序法
public boolean bubbleSortListDouble()
{
boolean status = true;
int low,high;
low = 0;
high = listLength - 1;
while (low<high && status)
{
status = false;
for (int i = low; i < high; i++)
{
if (element[i] > element[i + 1])
{
element[i] = element[i] ^ element[i + 1];
element[i + 1] = element[i] ^ element[i + 1];
element[i] = element[i] ^ element[i + 1];
status = true;
}
}
high--;
for (int j = high; j > low; j--)
{
if (element[j] < element[j - 1])
{
element[j] = element[j] ^ element[j - 1];
element[j - 1] = element[j] ^ element[j - 1];
element[j] = element[j] ^ element[j - 1];
status = true;
}
}
low++;
}
return true;
}
//堆排序
//构造堆
public boolean adjustHeap(int s,int length)
{
int temp = element[s];
for(int j=2*s+1;j<length;j=j*2+1) //下标时从零开始的
{
//比较左右孩子大小
if(element[j]<element[j+1] && j+1<length)
{
j++; //左孩子大于右孩子,指向右孩子
}
//双亲和孩子比较大小,双亲小则不作任何动作,双亲大则,右孩子上移到双亲位置
if(element[j]>temp)
{
element[s] = element[j];
s = j; //交换以后双亲节点转到下一层
}
else
{
break; //如果双亲小于孩子,和接束本轮筛选
}
}
element[s] = temp; //筛选的元素放入运算结果位置
return true;
}
public boolean heapSort()
{
//从[listLength/2]开始筛选
for(int i=(listLength/2)-1;i>=0;i--)
{
adjustHeap(i,listLength);
}
for(int j =listLength-1;j>0;j--)
{
//堆顶元素和堆尾元素交换
int temp = element[j];
element[j] = element[0];
element[0] = temp;
adjustHeap(0,j);
}
return true;
}
//插入排序
public boolean insertSort()
{
for(int i=1;i<listLength;i++)
{
int temp = element[i];
int j;
for(j=i-1;j>=0;j--)
{
System.out.println("temp =" +temp + "\tj=" + j);
if(element[j]>temp)
{
element[j+1] = element[j];
}
else
{
break;
}
}
System.out.println("test");
element[j+1] = temp;
printList();
}
return true;
}
//希尔排序
public boolean shellSort()
{
for(int step=listLength/2;step>0;step/=2) {
for (int i = 1; i < listLength; i+=step) {
int temp = element[i];
int j;
for (j = i - step; j >= 0; j-=step) {
if (element[j] > temp) {
element[j + step] = element[j];
} else {
break;
}
}
System.out.println("test");
element[j + step] = temp;
printList();
}
}
return true;
}
//折半排序
public boolean binaryInsetSort() {
for (int i = 1; i < listLength; i++) {
int temp = element[i];
int j;
int low, high, mid;
low = 0;
high = i-1;
while (low <= high) {
mid = (low + high) / 2;
System.out.println("test");
if (element[mid] > temp)
high = mid - 1;
else
low = mid + 1;
}
for (j = i - 1; j >= high+1; j--)
element[j + 1] = element[j];
element[high+1] = temp;
printList();
System.out.println("i=" + i);
}
return true;
}
//测试
public static void main(String[] args) throws Exception
{
int a[] = new int[]{1504,3,42,42,8,183,22,238,85,18,22,18,31};
List L = new List();
for(int i=0;i<a.length;i++)
{
L.insertList(i,a[i]);
}
L.printList();
//System.out.println(L.listLength-1);
//L.quickSortList(0,L.listLength-1);
//L.bubbleSortListDouble();
//L.heapSort();
//L.bubbleSortListDouble();
//L.insertSort();
//L.shellSort();
//L.binaryInsetSort();
L.quickSortListLeft(0,L.listLength-1);
L.printList();
System.out.println( "OK!\n");
}
}
JAVA从零开始之数据结构与算法线性表基本排序方法(1)
最新推荐文章于 2024-01-20 09:30:00 发布