一、二叉树排序
public class Test {
public static int[] a = { 0, 10, 32, 1, 9, 5, 7, 12, 2, 4, 3 };
public static int[] Data = new int[20];
public static void main(String args[]) {
int i;
int Index = 1; // 数据索引变量
BNTreeArray BNTree = new BNTreeArray(); // 声明二叉树数组
Data[Index] = a[Index];
BNTreeArray.TreeData[0] = Data[Index];
Index++;
for (i = 2; i < a.length; i++) {
Data[Index] = a[Index];
BNTree.Create(Data[Index]); // 建立二叉查找树
Index++;
}
System.out.print("排序前 : ");
for (i = 1; i < Index; i++)
System.out.print(" " + Data[i] + " ");
System.out.println("");
// 排序后结果
System.out.print("排序后 : ");
BNTreeArray.InOrder(0); // 中序遍历
}
}
class BNTreeArray {
public static int MaxSize = 20;
public static int[] TreeData = new int[MaxSize];
public static int[] RightNode = new int[MaxSize];
public static int[] LeftNode = new int[MaxSize];
public BNTreeArray() {
int i; // 循环计数变量
for (i = 0; i < MaxSize; i++) {
TreeData[i] = 0;
RightNode[i] = -1;
LeftNode[i] = -1;
}
}
public void Create(int Data) {
int i; // 循环计数变量
int Level = 0; // 树的阶层数
int Position = 0;
for (i = 0; TreeData[i] != 0; i++)
TreeData[i] = Data;
while (true) // 寻找节点位置
{
// 判断是左子树或是右子树
if (Data > TreeData[Level]) {
// 右树是否有下一阶层
if (RightNode[Level] != -1)
Level = RightNode[Level];
else {
Position = -1; // 设定为右树
break;
}
} else {
// 左树是否有下一阶层
if (LeftNode[Level] != -1)
Level = LeftNode[Level];
else {
Position = 1; // 设定为左树
break;
}
}
}
if (Position == 1) // 建立节点的左右连结
LeftNode[Level] = i; // 连结左子树
else
RightNode[Level] = i; // 连结右子树
}
public static void InOrder(int Pointer) {
if (Pointer != -1) // 遍历的终止条件
{
InOrder(LeftNode[Pointer]); // 处理左子树
System.out.print(" " + TreeData[Pointer] + " ");
InOrder(RightNode[Pointer]); // 处理右子树
}
}
}
public class Test {
public static int[] a = { 0, 10, 32, 1, 9, 5, 7, 12, 2, 4, 3 }; // 预设数据数组
public static int[] Data = new int[20]; // 预设数据数组
public static void main(String args[]) {
int i; // 循环计数变量
int Index = 1; // 数据索引变量
BNTreeArray BNTree = new BNTreeArray(); // 声明二叉树数组
Data[Index] = a[Index];
BNTreeArray.TreeData[0] = Data[Index];
Index++;
for (i = 2; i < a.length; i++) {
Data[Index] = a[Index];
BNTree.Create(Data[Index]); // 建立二叉查找树
Index++;
}
System.out.print("排序前 : ");
for (i = 1; i < Index; i++)
System.out.print(" " + Data[i] + " ");
System.out.println("");
System.out.print("排序后 : ");
BNTreeArray.InOrder(0); // 中序遍历
}
}
class BNTreeArray {
public static int MaxSize = 20;
public static int[] TreeData = new int[MaxSize];
public static int[] RightNode = new int[MaxSize];
public static int[] LeftNode = new int[MaxSize];
public BNTreeArray() {
int i; // 循环计数变量
for (i = 0; i < MaxSize; i++) {
TreeData[i] = 0;
RightNode[i] = -1;
LeftNode[i] = -1;
}
}
public void Create(int Data) {
int i; // 循环计数变量
int Level = 0; // 树的阶层数
int Position = 0;
for (i = 0; TreeData[i] != 0; i++)
TreeData[i] = Data;
while (true) // 寻找节点位置
{
// 判断是左子树或是右子树
if (Data > TreeData[Level]) {
if (RightNode[Level] != -1)
Level = RightNode[Level];
else {
Position = -1; // 设定为右树
break;
}
} else {
if (LeftNode[Level] != -1)
Level = LeftNode[Level];
else {
Position = 1; // 设定为左树
break;
}
}
}
if (Position == 1) // 建立节点的左右连结
LeftNode[Level] = i; // 连结左子树
else
RightNode[Level] = i; // 连结右子树
}
public static void InOrder(int Pointer) {
if (Pointer != -1) // 遍历的终止条件
{
InOrder(LeftNode[Pointer]); // 处理左子树
System.out.print(" " + TreeData[Pointer] + " ");
InOrder(RightNode[Pointer]); // 处理右子树
}
}
}二、插入排序
/**
public class Test {
public static int[] a = { 10, 32, 1, 9, 5, 7, 12, 0, 4, 3 }; // 预设数据数组
public static void main(String args[]) {
int i; // 循环计数变量
int Index = a.length;// 数据索引变量
System.out.print("排序前: ");
for (i = 0; i < Index - 1; i++)
System.out.print(" " + a[i] + " ");
System.out.println("");
InsertSort(Index - 1); // 选择排序
System.out.print("排序后: ");
for (i = 0; i < Index - 1; i++)
System.out.print(" " + a[i] + " ");
System.out.println("");
}
public static void InsertSort(int Index) {
int i, j, k; // 循环计数变量
int InsertNode; // 欲插入数据变量
for (i = 1; i < Index; i++) // 依序插入数值
{
InsertNode = a[i]; // 设定欲插入的数值
j = i - 1; // 欲插入数组的开始位置
while (j >= 0 && InsertNode < a[j]) {
a[j + 1] = a[j];
j--;
}
a[j + 1] = InsertNode; // 将数值插入
System.out.print("排序中: ");
for (k = 0; k < Index; k++)
System.out.print(" " + a[k] + " ");
System.out.println("");
}
}
}
public class Test {
public static int[] a = { 10, 32, 1, 9, 5, 7, 12, 0, 4, 3 }; // 预设数据数组
public static void main(String args[]) {
int i; // 循环计数变量
int Index = a.length;// 数据索引变量
System.out.print("排序前: ");
for (i = 0; i < Index - 1; i++)
System.out.print(" " + a[i] + " ");
System.out.println("");
InsertSort(Index - 1); // 选择排序
System.out.print("排序后: ");
for (i = 0; i < Index - 1; i++)
System.out.print(" " + a[i] + " ");
System.out.println("");
}
public static void InsertSort(int Index) {
int i, j, k; // 循环计数变量
int InsertNode; // 欲插入数据变量
for (i = 1; i < Index; i++) // 依序插入数值
{
InsertNode = a[i]; // 设定欲插入的数值
j = i - 1; // 欲插入数组的开始位置
while (j >= 0 && InsertNode < a[j]) {
a[j + 1] = a[j];
j--;
}
a[j + 1] = InsertNode; // 将数值插入
System.out.print("排序中: ");
for (k = 0; k < Index; k++)
System.out.print(" " + a[k] + " ");
System.out.println("");
}
}
}
三、希尔Shell排序
/public class Test {
public static int[] a = { 10, 32, 1, 9, 5, 7, 12, 0, 4, 3 }; // 预设数据数组
public static void main(String args[]) {
int i; // 循环计数变量
int Index = a.length;// 数据索引变量
System.out.print("排序前: ");
for (i = 0; i < Index - 1; i++)
System.out.printf("%3s ", a[i]);
System.out.println("");
ShellSort(Index - 1); // 选择排序
System.out.print("排序后: ");
for (i = 0; i < Index - 1; i++)
System.out.printf("%3s ", a[i]);
System.out.println("");
}
public static void ShellSort(int Index) {
int i, j, k; // 循环计数变量
int Temp; // 暂存变量
boolean Change; // 数据是否改变
int DataLength; // 分割集合的间隔长度
int Pointer; // 进行处理的位置
DataLength = (int) Index / 2; // 初始集合间隔长度
while (DataLength != 0) // 数列仍可进行分割
{
for (j = DataLength; j < Index; j++) {
Change = false;
Temp = a[j]; // 暂存Data[j]的值,待交换值时用
Pointer = j - DataLength; // 计算进行处理的位置
while (Temp < a[Pointer] && Pointer >= 0 && Pointer <= Index) {
a[Pointer + DataLength] = a[Pointer];
Change = true;
if (Pointer < 0 || Pointer > Index)
break;
}
a[Pointer + DataLength] = Temp;
if (Change) {
System.out.print("排序中: ");
for (k = 0; k < Index; k++)
System.out.printf("%3s ", a[k]);
System.out.println("");
}
}
DataLength = DataLength / 2; // 计算下次分割的间隔长度
}
}
}
public class Test {
public static int[] a = { 10, 32, 1, 9, 5, 7, 12, 0, 4, 3 }; // 预设数据数组
public static void main(String args[]) {
int i; // 循环计数变量
int Index = a.length;// 数据索引变量
System.out.print("排序前: ");
for (i = 0; i < Index - 1; i++)
System.out.printf("%3s ", a[i]);
System.out.println("");
ShellSort(Index - 1); // 选择排序
// 排序后结果
System.out.print("排序后: ");
for (i = 0; i < Index - 1; i++)
System.out.printf("%3s ", a[i]);
System.out.println("");
}
public static void ShellSort(int Index) {
int i, j, k; // 循环计数变量
int Temp; // 暂存变量
boolean Change; // 数据是否改变
int DataLength; // 分割集合的间隔长度
int Pointer; // 进行处理的位置
DataLength = (int) Index / 2; // 初始集合间隔长度
while (DataLength != 0) // 数列仍可进行分割
{
for (j = DataLength; j < Index; j++) {
Change = false;
Temp = a[j]; // 暂存Data[j]的值,待交换值时用
Pointer = j - DataLength; // 计算进行处理的位置
while (Temp < a[Pointer] && Pointer >= 0 && Pointer <= Index) {
a[Pointer + DataLength] = a[Pointer];
Pointer = Pointer - DataLength;
Change = true;
if (Pointer < 0 || Pointer > Index)
break;
}
a[Pointer + DataLength] = Temp;
if (Change) {
System.out.print("排序中: ");
for (k = 0; k < Index; k++)
System.out.printf("%3s ", a[k]);
System.out.println("");
}
}
DataLength = DataLength / 2; // 计算下次分割的间隔长度
}
}
}
public class Test {
public static int[] a = { 0, 10, 32, 1, 9, 5, 7, 12, 2, 4, 3 };
public static int[] Data = new int[20];
public static void main(String args[]) {
int i;
int Index = 1; // 数据索引变量
BNTreeArray BNTree = new BNTreeArray(); // 声明二叉树数组
Data[Index] = a[Index];
BNTreeArray.TreeData[0] = Data[Index];
Index++;
for (i = 2; i < a.length; i++) {
Data[Index] = a[Index];
BNTree.Create(Data[Index]); // 建立二叉查找树
Index++;
}
System.out.print("排序前 : ");
for (i = 1; i < Index; i++)
System.out.print(" " + Data[i] + " ");
System.out.println("");
// 排序后结果
System.out.print("排序后 : ");
BNTreeArray.InOrder(0); // 中序遍历
}
}
class BNTreeArray {
public static int MaxSize = 20;
public static int[] TreeData = new int[MaxSize];
public static int[] RightNode = new int[MaxSize];
public static int[] LeftNode = new int[MaxSize];
public BNTreeArray() {
int i; // 循环计数变量
for (i = 0; i < MaxSize; i++) {
TreeData[i] = 0;
RightNode[i] = -1;
LeftNode[i] = -1;
}
}
public void Create(int Data) {
int i; // 循环计数变量
int Level = 0; // 树的阶层数
int Position = 0;
for (i = 0; TreeData[i] != 0; i++)
TreeData[i] = Data;
while (true) // 寻找节点位置
{
// 判断是左子树或是右子树
if (Data > TreeData[Level]) {
// 右树是否有下一阶层
if (RightNode[Level] != -1)
Level = RightNode[Level];
else {
Position = -1; // 设定为右树
break;
}
} else {
// 左树是否有下一阶层
if (LeftNode[Level] != -1)
Level = LeftNode[Level];
else {
Position = 1; // 设定为左树
break;
}
}
}
if (Position == 1) // 建立节点的左右连结
LeftNode[Level] = i; // 连结左子树
else
RightNode[Level] = i; // 连结右子树
}
public static void InOrder(int Pointer) {
if (Pointer != -1) // 遍历的终止条件
{
InOrder(LeftNode[Pointer]); // 处理左子树
System.out.print(" " + TreeData[Pointer] + " ");
InOrder(RightNode[Pointer]); // 处理右子树
}
}
}
public class Test {
public static int[] a = { 0, 10, 32, 1, 9, 5, 7, 12, 2, 4, 3 }; // 预设数据数组
public static int[] Data = new int[20]; // 预设数据数组
public static void main(String args[]) {
int i; // 循环计数变量
int Index = 1; // 数据索引变量
BNTreeArray BNTree = new BNTreeArray(); // 声明二叉树数组
Data[Index] = a[Index];
BNTreeArray.TreeData[0] = Data[Index];
Index++;
for (i = 2; i < a.length; i++) {
Data[Index] = a[Index];
BNTree.Create(Data[Index]); // 建立二叉查找树
Index++;
}
System.out.print("排序前 : ");
for (i = 1; i < Index; i++)
System.out.print(" " + Data[i] + " ");
System.out.println("");
System.out.print("排序后 : ");
BNTreeArray.InOrder(0); // 中序遍历
}
}
class BNTreeArray {
public static int MaxSize = 20;
public static int[] TreeData = new int[MaxSize];
public static int[] RightNode = new int[MaxSize];
public static int[] LeftNode = new int[MaxSize];
public BNTreeArray() {
int i; // 循环计数变量
for (i = 0; i < MaxSize; i++) {
TreeData[i] = 0;
RightNode[i] = -1;
LeftNode[i] = -1;
}
}
public void Create(int Data) {
int i; // 循环计数变量
int Level = 0; // 树的阶层数
int Position = 0;
for (i = 0; TreeData[i] != 0; i++)
TreeData[i] = Data;
while (true) // 寻找节点位置
{
// 判断是左子树或是右子树
if (Data > TreeData[Level]) {
if (RightNode[Level] != -1)
Level = RightNode[Level];
else {
Position = -1; // 设定为右树
break;
}
} else {
if (LeftNode[Level] != -1)
Level = LeftNode[Level];
else {
Position = 1; // 设定为左树
break;
}
}
}
if (Position == 1) // 建立节点的左右连结
LeftNode[Level] = i; // 连结左子树
else
RightNode[Level] = i; // 连结右子树
}
public static void InOrder(int Pointer) {
if (Pointer != -1) // 遍历的终止条件
{
InOrder(LeftNode[Pointer]); // 处理左子树
System.out.print(" " + TreeData[Pointer] + " ");
InOrder(RightNode[Pointer]); // 处理右子树
}
}
}二、插入排序
/**
public class Test {
public static int[] a = { 10, 32, 1, 9, 5, 7, 12, 0, 4, 3 }; // 预设数据数组
public static void main(String args[]) {
int i; // 循环计数变量
int Index = a.length;// 数据索引变量
System.out.print("排序前: ");
for (i = 0; i < Index - 1; i++)
System.out.print(" " + a[i] + " ");
System.out.println("");
InsertSort(Index - 1); // 选择排序
System.out.print("排序后: ");
for (i = 0; i < Index - 1; i++)
System.out.print(" " + a[i] + " ");
System.out.println("");
}
public static void InsertSort(int Index) {
int i, j, k; // 循环计数变量
int InsertNode; // 欲插入数据变量
for (i = 1; i < Index; i++) // 依序插入数值
{
InsertNode = a[i]; // 设定欲插入的数值
j = i - 1; // 欲插入数组的开始位置
while (j >= 0 && InsertNode < a[j]) {
a[j + 1] = a[j];
j--;
}
a[j + 1] = InsertNode; // 将数值插入
System.out.print("排序中: ");
for (k = 0; k < Index; k++)
System.out.print(" " + a[k] + " ");
System.out.println("");
}
}
}
public class Test {
public static int[] a = { 10, 32, 1, 9, 5, 7, 12, 0, 4, 3 }; // 预设数据数组
public static void main(String args[]) {
int i; // 循环计数变量
int Index = a.length;// 数据索引变量
System.out.print("排序前: ");
for (i = 0; i < Index - 1; i++)
System.out.print(" " + a[i] + " ");
System.out.println("");
InsertSort(Index - 1); // 选择排序
System.out.print("排序后: ");
for (i = 0; i < Index - 1; i++)
System.out.print(" " + a[i] + " ");
System.out.println("");
}
public static void InsertSort(int Index) {
int i, j, k; // 循环计数变量
int InsertNode; // 欲插入数据变量
for (i = 1; i < Index; i++) // 依序插入数值
{
InsertNode = a[i]; // 设定欲插入的数值
j = i - 1; // 欲插入数组的开始位置
while (j >= 0 && InsertNode < a[j]) {
a[j + 1] = a[j];
j--;
}
a[j + 1] = InsertNode; // 将数值插入
System.out.print("排序中: ");
for (k = 0; k < Index; k++)
System.out.print(" " + a[k] + " ");
System.out.println("");
}
}
}
三、希尔Shell排序
/public class Test {
public static int[] a = { 10, 32, 1, 9, 5, 7, 12, 0, 4, 3 }; // 预设数据数组
public static void main(String args[]) {
int i; // 循环计数变量
int Index = a.length;// 数据索引变量
System.out.print("排序前: ");
for (i = 0; i < Index - 1; i++)
System.out.printf("%3s ", a[i]);
System.out.println("");
ShellSort(Index - 1); // 选择排序
System.out.print("排序后: ");
for (i = 0; i < Index - 1; i++)
System.out.printf("%3s ", a[i]);
System.out.println("");
}
public static void ShellSort(int Index) {
int i, j, k; // 循环计数变量
int Temp; // 暂存变量
boolean Change; // 数据是否改变
int DataLength; // 分割集合的间隔长度
int Pointer; // 进行处理的位置
DataLength = (int) Index / 2; // 初始集合间隔长度
while (DataLength != 0) // 数列仍可进行分割
{
for (j = DataLength; j < Index; j++) {
Change = false;
Temp = a[j]; // 暂存Data[j]的值,待交换值时用
Pointer = j - DataLength; // 计算进行处理的位置
while (Temp < a[Pointer] && Pointer >= 0 && Pointer <= Index) {
a[Pointer + DataLength] = a[Pointer];
Change = true;
if (Pointer < 0 || Pointer > Index)
break;
}
a[Pointer + DataLength] = Temp;
if (Change) {
System.out.print("排序中: ");
for (k = 0; k < Index; k++)
System.out.printf("%3s ", a[k]);
System.out.println("");
}
}
DataLength = DataLength / 2; // 计算下次分割的间隔长度
}
}
}
public class Test {
public static int[] a = { 10, 32, 1, 9, 5, 7, 12, 0, 4, 3 }; // 预设数据数组
public static void main(String args[]) {
int i; // 循环计数变量
int Index = a.length;// 数据索引变量
System.out.print("排序前: ");
for (i = 0; i < Index - 1; i++)
System.out.printf("%3s ", a[i]);
System.out.println("");
ShellSort(Index - 1); // 选择排序
// 排序后结果
System.out.print("排序后: ");
for (i = 0; i < Index - 1; i++)
System.out.printf("%3s ", a[i]);
System.out.println("");
}
public static void ShellSort(int Index) {
int i, j, k; // 循环计数变量
int Temp; // 暂存变量
boolean Change; // 数据是否改变
int DataLength; // 分割集合的间隔长度
int Pointer; // 进行处理的位置
DataLength = (int) Index / 2; // 初始集合间隔长度
while (DataLength != 0) // 数列仍可进行分割
{
for (j = DataLength; j < Index; j++) {
Change = false;
Temp = a[j]; // 暂存Data[j]的值,待交换值时用
Pointer = j - DataLength; // 计算进行处理的位置
while (Temp < a[Pointer] && Pointer >= 0 && Pointer <= Index) {
a[Pointer + DataLength] = a[Pointer];
Pointer = Pointer - DataLength;
Change = true;
if (Pointer < 0 || Pointer > Index)
break;
}
a[Pointer + DataLength] = Temp;
if (Change) {
System.out.print("排序中: ");
for (k = 0; k < Index; k++)
System.out.printf("%3s ", a[k]);
System.out.println("");
}
}
DataLength = DataLength / 2; // 计算下次分割的间隔长度
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货