《算法笔记》4.6小节——算法初步->two pointers

A 二路归并排序(mergesort)递归法 [2*+]

B 基础排序III:归并排序

题目描述

归并排序是一个时间复杂度为O(nlogn)的算法,对于大量数据远远优于冒泡排序与插入排序。

这是一道排序练习题,数据量较大,请使用归并排序完成。

输入

第一行一个数字n,代表输入的组数

其后每组第一行输入一个数字m,代表待排序数字的个数

其后m行每行一个数据,大小在1~100000之间,互不相等,最多有10万个数据。

输出

升序输出排好序的数据,每行一个数字

样例输入

1
10
10
9
8
7
6
5
4
3
2
1

样例输出

1
2
3
4
5
6
7
8
9
10

代码提交

#include<stdio.h>
#include<string.h>

using namespace std;

int a[200000];

void merge(int a[], int l1, int r1, int l2, int r2) {
    int i = l1, j = l2;
    int temp[400000], index = 0;//用于临时存放合并后的数组
    while (i <= r1 && j <= r2) {
        if (a[i] <= a[j]) temp[index++] = a[i++];
        else temp[index++] = a[j++];
    }
    while (i <= r1) temp[index++] = a[i++];
    while (j <= r2) temp[index++] = a[j++];
    for (int i = 0; i < index; i++) a[l1 + i] = temp[i];
}

void mergeSort(int a[], int left, int right) {
    if (left < right) {
        int mid = (left + right) / 2;
        mergeSort(a, left, mid);
        mergeSort(a, mid + 1, right);
        merge(a, left, mid, mid + 1, right);
    }
}

int main() {
    int n,m;
    scanf("%d",&n);
    while(n--){
        memset(a,0,sizeof(a));
        scanf("%d",&m);
        for(int i=0;i<m;i++){
            scanf("%d",&a[i]);
        }
        mergeSort(a,0,m-1);
        for(int i=0;i<m;i++){
            printf("%d\n",a[i]);
        }
    }
    return 0;
}

C 快速排序 qsort [2*]

题目描述

输入n个整数,用快速排序的方法进行排序

Input

第一行数字n 代表接下来有n个整数
接下来n行,每行一个整数

Output

升序输出排序结果
每行一个数据

Sample Input

5
12
18
14
13
16

Sample Output

12
13
14
16
18

Hint

n<=5000
每个数据<=5000

代码提交

#pragma warning(disable:4996);
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<time.h>
#include<stdlib.h>
using namespace std;
int a[100005];
int partition(int a[], int left, int right) {
	int temp = a[left];
	while (left < right) {
		while (left<right && a[right]>temp) right--;
		a[left] = a[right];
		while (left<right && a[left]<=temp) left++;
		a[right] = a[left];
	}
	a[left] = temp;
	return left;
}
void quicksort(int a[], int left, int right) {
	if (left < right) {
		int pos = partition(a, left, right);
		quicksort(a, left, pos - 1);
		quicksort(a, pos + 1, right);
	}
}
int main() {
	int n;
	while (scanf("%d", &n) != EOF) {
		for (int i = 0;i < n;i++)
		{
			scanf("%d", &a[i]);
		}
		quicksort(a, 0, n - 1);
		for (int i = 0;i < n;i++) {
			printf("%d\n", a[i]);
		}
	}
	return 0;
}



D 二分递归快排(Qsort) [2*]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
To solve this problem, we can follow the following steps: 1. Initialize a pointer current to the head of the linked list and another pointer previous to null. 2. Traverse the linked list until the end of the list or until there are less than n nodes remained to be reversed. 3. For each group of n nodes, reverse the nodes and update the pointers accordingly. 4. If there are less than n nodes remaining, reverse them as well. 5. Return the updated head of the linked list. Here is the Python code that implements the above algorithm: ```python class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next def reverseLinkedList(head, n): current = head previous = None while current: last_node_of_previous_part = previous last_node_of_sub_list = current i = 0 while current and i < n: next_node = current.next current.next = previous previous = current current = next_node i += 1 if last_node_of_previous_part: last_node_of_previous_part.next = previous else: head = previous last_node_of_sub_list.next = current previous = last_node_of_sub_list return head ``` Here is an example usage of the above function: ```python # create input linked list: A -> B -> C -> D -> E -> F -> G -> H -> I a = ListNode("A") b = ListNode("B") c = ListNode("C") d = ListNode("D") e = ListNode("E") f = ListNode("F") g = ListNode("G") h = ListNode("H") i = ListNode("I") a.next = b b.next = c c.next = d d.next = e e.next = f f.next = g g.next = h h.next = i # reverse the linked list in groups of 3 nodes head = reverseLinkedList(a, 3) # print the updated linked list: A -> D -> C -> B -> G -> F -> E -> I -> H current = head while current: print(current.val, end=" -> ") current = current.next print("None") ``` Output: ``` A -> D -> C -> B -> G -> F -> E -> I -> H -> None ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值