双向链表上的快速排序

2024年8月28日 | 阅读 18 分钟

在为双向链表实现快速排序之前,让我们先了解一下快速排序。快速排序是另一种使用分治法实现的排序算法。由于其在平均情况下的高性能(n log n),快速排序也是一种有用的排序算法选项。与归并排序不同,快速排序在排序过程中不使用额外的数组,虽然其平均情况与归并排序相同,但在快速排序的情况下,(n log n) 的隐藏因子通常更低。

快速排序首先选择一个枢轴,然后围绕它对数组进行分区。所有小于枢轴的项放在一边,而所有大于枢轴的元素放在另一边。这个分区过程对较小的子数组重复进行,从而得到一个已排序的数组。选择枢轴是分区的第一步。在我们的算法中,我们将始终使用数组的最后一个成员作为枢轴,我们将专注于快速排序的概念。枢轴可以通过多种方式选择,包括元素的中间值、数组中的第一个成员、随机元素等。

在选择枢轴之后,我们必须将所有小于枢轴的元素放在一边,所有大于枢轴的元素放在另一边。我们将通过遍历数组并什么也不做来实现这一点。

  • 如果我们遇到一个大于枢轴的元素,那么将形成一个大于枢轴的元素簇。
  • 现在,任何小于枢轴的元素都将与较大元素簇的第一个元素交换。
  • 因此,我们将创建另一个小于枢轴的元素簇。
  • 最后,我们将枢轴与较大元素簇中的第一个成员交换。

C++ 代码

现在让我们编写代码,借助快速排序算法对双向链表数据结构执行排序操作。首先,让我们编写一个 C++ 代码来对双向链表数据结构执行快速排序。

输出

上述代码给出以下输出。

Select one of the operations::
1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Quicksort on the Doubly Linked List.
1
Enter the value of the node to be inserted
20

Do you want to continue (Type y or n) 
y

Select one of the operations::
1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Quicksort on the Doubly Linked List.
1
Enter the value of the node to be inserted
30

Do you want to continue (Type y or n) 
y

Select one of the operations::
1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Quicksort on the Doubly Linked List.
1 
Enter the value of the node to be inserted
12

Do you want to continue (Type y or n) 
y

Select one of the operations::
1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Quicksort on the Doubly Linked List.
1
Enter the value of the node to be inserted
3

Do you want to continue (Type y or n) 
y

Select one of the operations::
1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Quicksort on the Doubly Linked List.
1 
Enter the value of the node to be inserted
77

Do you want to continue (Type y or n) 
y

Select one of the operations::
1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Quicksort on the Doubly Linked List.
1
Enter the value of the node to be inserted
54

Do you want to continue (Type y or n) 
y

Select one of the operations::
1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Quicksort on the Doubly Linked List.
2
Contents of the Doubly Linked List are::
54 77 3 12 30 20 

Do you want to continue (Type y or n) 
y

Select one of the operations::
1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Quicksort on the Doubly Linked List.
3
Quicksort applied successfully on the Doubly Linked List.

Do you want to continue (Type y or n) 
y

Select one of the operations::
1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Quicksort on the Doubly Linked List.
2
Contents of the Doubly Linked List are::
3 12 20 30 54 77 

Do you want to continue (Type y or n) 
n

通过这种方式,我们编写了一个 C++ 代码来对双向链表数据结构执行快速排序操作。为双向链表数据结构编写了三个函数,一个是向双向链表添加新节点,第二个是显示双向链表的所有节点并显示与这些节点关联的值,第三个函数是对双向链表数据结构执行快速排序操作。

用户首先在双向链表数据结构中添加足够数量的节点。节点成功添加后,通过从每次操作后显示的菜单中选择第三个选项,对双向链表数据结构执行快速排序操作,这将调用代码中编写的 Quicksort () 函数,并将双向链表的根节点作为参数传递。

完成快速排序操作后,用户可以通过选择用户的第二个选项来显示双向链表中节点的值,从而确认快速排序操作的结果。所有操作完成后,用户可以通过输入“n”或“N”字符退出代码。

Java 代码

让我们编写一个 Java 代码来对双向链表数据结构执行快速排序。

输出

上述 Java 代码执行后会给出以下输出。

Select one of the operations::

1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Quicksort on the Doubly Linked List.
1
Enter integer element to insert
265

Do you want to continue (Type y or n) 

y

Select one of the operations::

1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Quicksort on the Doubly Linked List.
1
Enter integer element to insert
400 

Do you want to continue (Type y or n) 

y

Select one of the operations::

1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Quicksort on the Doubly Linked List.
1
Enter integer element to insert
133

Do you want to continue (Type y or n) 

y

Select one of the operations::

1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Quicksort on the Doubly Linked List.
1
Enter integer element to insert
879

Do you want to continue (Type y or n) 

y

Select one of the operations::

1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Quicksort on the Doubly Linked List.
1
Enter integer element to insert
228

Do you want to continue (Type y or n) 

y

Select one of the operations::

1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Quicksort on the Doubly Linked List.
1
Enter integer element to insert
609

Do you want to continue (Type y or n) 

y

Select one of the operations::

1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Quicksort on the Doubly Linked List.
1
Enter integer element to insert
551

Do you want to continue (Type y or n) 

y

Select one of the operations::

1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Quicksort on the Doubly Linked List.
2
Doubly Linked List::
551 609 228 879 133 400 265 
Do you want to continue (Type y or n) 

y

Select one of the operations::

1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Quicksort on the Doubly Linked List.
1
Enter integer element to insert
23

Do you want to continue (Type y or n) 

y

Select one of the operations::

1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Quicksort on the Doubly Linked List.
2
Doubly Linked List::
23 551 609 228 879 133 400 265 
Do you want to continue (Type y or n) 

y

Select one of the operations::

1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Quicksort on the Doubly Linked List.
3
Quicksort done.

Do you want to continue (Type y or n) 

y

Select one of the operations::

1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Quicksort on the Doubly Linked List.
2
Doubly Linked List::
23 133 228 265 400 551 609 879 
Do you want to continue (Type y or n) 

N

通过这种方式,我们编写了一个 Java 代码来对双向链表数据结构执行快速排序操作。为双向链表数据结构编写了三个函数,一个是向双向链表添加新节点,第二个是显示双向链表的所有节点并显示与这些节点关联的值,最后一个函数是对双向链表数据结构执行快速排序操作。

用户首先在双向链表数据结构中添加足够数量的节点。节点成功添加后,通过从每次操作后显示的菜单中选择第三个选项,对双向链表数据结构执行快速排序操作,这将调用代码中编写的 Quicksort () 函数,并将双向链表的根节点作为参数传递。

完成快速排序操作后,用户可以通过选择用户的第二个选项来显示双向链表中节点的值,从而确认快速排序操作的结果。所有操作完成后,用户可以通过输入“n”或“N”字符退出代码。

C 代码

让我们编写一个 C 代码来对双向链表数据结构执行快速排序。

输出

上述 C 代码会给出以下输出。

Select one of the operations::
1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Quicksort on the Doubly Linked List.
1

Enter the value of the node to be inserted
11

Do you want to continue (Type y or n)
y

Select one of the operations::
1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Quicksort on the Doubly Linked List.
1

Enter the value of the node to be inserted
65

Do you want to continue (Type y or n)
y

Select one of the operations::
1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Quicksort on the Doubly Linked List.
1

Enter the value of the node to be inserted
32

Do you want to continue (Type y or n)
y

Select one of the operations::
1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Quicksort on the Doubly Linked List.
1

Enter the value of the node to be inserted
9 

Do you want to continue (Type y or n)
y

Select one of the operations::
1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Quicksort on the Doubly Linked List.
2

Contents of the Doubly Linked List are::
9  32  65  11  

Do you want to continue (Type y or n)
y

Select one of the operations::
1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Quicksort on the Doubly Linked List.
1

Enter the value of the node to be inserted
522

Do you want to continue (Type y or n)
y

Select one of the operations::
1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Quicksort on the Doubly Linked List.
2

Contents of the Doubly Linked List are::
522  9  32  65  11  

Do you want to continue (Type y or n)
y

Select one of the operations::
1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Quicksort on the Doubly Linked List.
3

Quicksort applied successfully on the Doubly Linked List.

Do you want to continue (Type y or n)
y

Select one of the operations::
1. To insert a new node in the Doubly Linked List.
2. To display the nodes of the Doubly Linked List.
3. To perform Quicksort on the Doubly Linked List.
2

Contents of the Doubly Linked List are::
9  11  32  65  522  

Do you want to continue (Type y or n)
n

通过这种方式,我们编写了一个 C 代码来对双向链表数据结构执行快速排序操作。为双向链表数据结构编写了三个函数,一个是向双向链表添加新节点,第二个是显示双向链表的所有节点并显示与这些节点关联的值,第三个函数是对双向链表数据结构执行快速排序操作。

用户首先在双向链表数据结构中添加足够数量的节点。节点成功添加后,通过从每次操作后显示的菜单中选择第三个选项,对双向链表数据结构执行快速排序操作,这将调用代码中编写的 Quicksort () 函数,并将双向链表的根节点作为参数传递。

完成快速排序操作后,用户可以通过选择用户的第二个选项来显示双向链表中节点的值,从而确认快速排序操作的结果。所有操作完成后,用户可以通过输入“n”或“N”字符退出代码。


下一主题逆序对计数