# 在 Java 中的氣泡排序演算法對手動連結串列進行排序

Sarwan Soomro 2024年2月15日

## Java 中的氣泡排序

``````for (int A = 0; A < sort - 1; A++)
for (int B = 0; B < sort - A - 1; B++)
if (DEMO[B] > DEMO[B + 1]) {
// Swapping of array
int temp = DEMO[B];
DEMO[B] = DEMO[B + 1];
DEMO[B + 1] = temp;
}
``````

``````// In this program, we will sort an array DEMO using the bubble sort algorithm
// Main class
// Main function
private void bubbleSort(int DEMO[]) {
// Using .length to determine entire length of array's index
int sort = DEMO.length;
// If array's length is less than int sort, increase it by 1
for (int A = 0; A < sort - 1; A++)
// Formula 1
for (int B = 0; B < sort - A - 1; B++)
if (DEMO[B] > DEMO[B + 1]) {
// Swapping of array
int temp = DEMO[B];
DEMO[B] = DEMO[B + 1];
DEMO[B + 1] = temp;
}
}
/* Now we are going to print DEMO array*/
void printArray(int DEMO[]) {
int sort = DEMO.length;
for (int A = 0; A < sort; ++A) System.out.print(DEMO[A] + " ");
System.out.println();
}
// We are going to implement a driver algorithm for sorting our DEMO array
public static void main(String args[]) {
int DEMO[] = {43, 65, 21, 64, 12, 6, 1};
ob.bubbleSort(DEMO);
System.out.println("After the array has been sorted!");
ob.printArray(DEMO);
}
}
``````

``````After the array has been sorted!
1 6 12 21 43 64 65
``````

## Java 中的類排序連結串列

``````class SortLL {
public static class Mynode {
int indx;
Mynode fwdMynode;

public Mynode(int indx) {
this.indx = indx;
this.fwdMynode = null;
}
public int getindx() {
return this.indx;
}
}
``````

1. 排序之前的連結串列：

程式碼：

``````class SortLL {
public static class Mynode {
int indx;
Mynode fwdMynode;

public Mynode(int indx) {
this.indx = indx;
this.fwdMynode = null;
}
public int getindx() {
return this.indx;
}
}
// My node class
private int size;
public SortLL() {
this.size = 0;
}
Mynode Mynode = new Mynode(indx);
} else {
while (CN.fwdMynode != null) {
CN = CN.fwdMynode;
}
CN.fwdMynode = Mynode;
}
size++;
}
public void sort() {
if (size > 1) {
boolean dtr;
do {
dtr = false;

while (fwd != null) {
if (thisMynode.indx > fwd.indx) {
dtr = true;
Mynode sig = fwd.fwdMynode;

fwd.fwdMynode = thisMynode;
thisMynode.fwdMynode = sig;
} else {
Mynode sig = fwd.fwdMynode;
fwd.fwdMynode = thisMynode;
thisMynode.fwdMynode = sig;
}
fwd = thisMynode.fwdMynode;
} else {
thisMynode = fwd;
fwd = fwd.fwdMynode;
}
}
} while (dtr);
}
}
public int listSize() {
return size;
}
public void printindx() {

while (CN != null) {
int indx = CN.getindx();
System.out.print(indx + " ");
CN = CN.fwdMynode;
}
System.out.println();
}
public boolean isEmpty() {
return size == 0;
}
}
// indxInterface class
class SrtBubb {
public static void main(String[] args) {
SortLL s = new SortLL();
System.out.println("Before Performing Bubble Sort");
s.printindx();
s.sort();
System.out.println("After Performing Bubble Sort");
s.printindx();
System.out.println("Size of the linked list is: " + s.listSize());
}
}
``````
2. 執行手動氣泡排序後：

輸出：

``````Before Performing Bubble Sort
12 2 7 19 23 9
After Performing Bubble Sort
2 7 9 12 19 23
Size of the linked list is: 6
``````

Sarwan Soomro is a freelance software engineer and an expert technical writer who loves writing and coding. He has 5 years of web development and 3 years of professional writing experience, and an MSs in computer science. In addition, he has numerous professional qualifications in the cloud, database, desktop, and online technologies. And has developed multi-technology programming guides for beginners and published many tech articles.