排序算法汇总
快速排序
void quickSort(vector<int>& arr, int l, int r) {
if (l >= r) return;
int i = l, j = r;
while (i < j) {
while (i < j && arr[j] >= arr[l]) j--;
while (i < j && arr[i] <= arr[l]) i++;
swap(arr[i], arr[j]);
}
swap(arr[i], arr[l]);
quickSort(arr, l, i - 1);
quickSort(arr, i + 1, r);
}
};
![在这里插入图片描述](https://img-blog.csdnimg.cn/7950c9c2196f451ab3c8c288b55e0249.png)
快速排序详解
归并排序
void mergesort(vector<int>& arr, vector<int> tmp, int start, int end)
{
if (start >= end)
{
return;
}
int mid = (start + end) >> 1;
mergesort(arr, tmp, start, mid);
mergesort(arr, tmp, mid + 1, end);
int pos = start;
int left = start, right = mid + 1;
while (left <= mid && right <= end)
{
tmp[pos++] = arr[left] <= arr[right] ? arr[left++] : arr[right++];
}
while (left <= mid)
{
tmp[pos++] = arr[left++];
}
while (right <= end)
{
tmp[pos++] = arr[right++];
}
for (int i = start; i <= end; i++)
{
arr[i] = tmp[i];
}
}
int main()
{
vector<int> arr = { 3,7,0,1,5 }, tmp(arr.size());
mergesort(arr, tmp, 0, arr.size() - 1);
return 0;
![在这里插入图片描述](https://img-blog.csdnimg.cn/67ab2e31f4fa48038f24df22fb336a95.png)
归并排序详解
归并排序(链表)
ListNode* sortList(ListNode* head) {
if(!head) return head;
int length = 0;
ListNode *node = head;
while(node)
{
length++;
node = node->next;
}
ListNode *dummyhead = new ListNode(0,head);
for(int sublength = 1; sublength < length; sublength <<= 1)
{
ListNode *prev = dummyhead, *cur = dummyhead->next;
while(cur)
{
ListNode *head1 = cur;
for(int i = 1; i < sublength && cur->next; i++)
{
cur = cur->next;
}
ListNode *head2 = cur->next;
cur->next = nullptr;
cur = head2;
for(int i = 1; i<sublength && cur && cur->next; i++)
{
cur = cur->next;
}
ListNode *next = nullptr;
if(cur)
{
next = cur->next;
cur->next = nullptr;
}
cur = next;
ListNode *merged = merge(head1, head2);
prev->next = merged;
while(prev->next)
{
prev = prev->next;
}
}
}
return dummyhead->next;
}
ListNode *merge(ListNode *headA, ListNode *headB)
{
ListNode *dummyhead = new ListNode(0);
ListNode *cur = dummyhead, *p1 = headA, *p2 = headB;
while(p1 && p2)
{
if(p1->val <= p2->val)
{
cur->next = p1;
p1 = p1->next;
}
else
{
cur->next = p2;
p2 = p2->next;
}
cur = cur->next;
}
cur->next = p1 ? p1 : p2;
return dummyhead->next;
}
class Solution {
public:
ListNode* sortList(ListNode* head) {
return sortList(head, nullptr);
}
ListNode* sortList(ListNode* head, ListNode* tail) {
if (head == nullptr) {
return head;
}
if (head->next == tail) {
head->next = nullptr;
return head;
}
ListNode* slow = head, *fast = head;
while (fast != tail) {
slow = slow->next;
fast = fast->next;
if (fast != tail) {
fast = fast->next;
}
}
ListNode* mid = slow;
return merge(sortList(head, mid), sortList(mid, tail));
}
ListNode* merge(ListNode* head1, ListNode* head2) {
ListNode* dummyHead = new ListNode(0);
ListNode* temp = dummyHead, *temp1 = head1, *temp2 = head2;
while (temp1 != nullptr && temp2 != nullptr) {
if (temp1->val <= temp2->val) {
temp->next = temp1;
temp1 = temp1->next;
} else {
temp->next = temp2;
temp2 = temp2->next;
}
temp = temp->next;
}
if (temp1 != nullptr) {
temp->next = temp1;
} else if (temp2 != nullptr) {
temp->next = temp2;
}
return dummyHead->next;
}
};
排序链表详解