归并排序
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
void merge(int a[], int l, int r, int mid)
{
int* aux = new int[r - l + 1];
int i, j, k;
for (k = l; k <= r; k++)
aux[k - l] = a[k];
i = l;
j = mid + 1;
for (k = l; k <= r; k++)
{
if (i > mid)
{
a[k] = aux[j - l];
j++;
}
else if (j > r)
{
a[k] = aux[i - l];
i++;
}
else if (aux[i - l] > aux[j - l])
{
a[k] = aux[j - l];
j++;
}
else
{
a[k] = aux[i - l];
i++;
}
}
}
void merge_sort(int a[], int l, int r)
{
if (l >= r)
return;
int mid = (l + r) / 2;
merge_sort(a, l, mid);
merge_sort(a, mid + 1, r);
merge(a, l, r, mid);
}
void mergesort(int a[], int l, int r)
{
merge_sort(a, l, r - 1);
}
int main()
{
int a[105], n, i;
cin >> n;
for (i = 0; i < n; i++)
cin >> a[i];
mergesort(a, 0, n);
for (i = 0; i < n; i++)
cout << a[i];
return 0;
}
快速排序
#include<iostream>
using namespace std;
int a[20];
void quicksort(int left, int right)
{
int i, j, t, temp;
if (left > right)
return;
temp = a[left];//存储基准数
i = left;
j = right;
while (i != j)
{
while (a[j] >= temp && i < j)//先j--:
j--;
while (a[i] <= temp && i < j)
i++;
if (i < j)//找的比基准数大的和小的
{
t = a[i];
a[i] = a[j];//交换位置里的元素;
a[j] = t;
}
}
a[left] = a[i];
a[i] = temp;
//递归
quicksort(left, i - 1);
quicksort(i + 1, right);
return;
}
int main() {
int i, j;
int n;
cin >> n;
for (i = 1; i <= n; i++)
{
cin >> a[i];
}
quicksort(1, n);
for (i = 1; i <= n; i++)
{
cout << a[i];
}
}
循环日程表
import java.util.Scanner;
public class xunhuansai {
public static void main(String[] args) {
System.out.println("请输入一个数:");
Scanner input = new Scanner(System.in);
int k = input.nextInt();
System.out.println("以下表格横为天数,纵为组数");
int num = (int) Math.pow(2,k);
System.out.print(" ");
for(int i = 1;i<num;i++){
System.out.print(i);
System.out.print("\t");
}
System.out.println();
int [][]schedule = Schedule(num,k);
for(int i = 0;i<schedule.length;i++){
for(int j = 0;j<schedule.length;j++){
System.out.print(schedule[i][j]);
System.out.print("\t");
}
System.out.println();
}
}
public static int[][] Schedule(int num,int k ){
int [][]schedule = new int [num][num];
for(int i = 0;i<num;i++){
for(int j = 0;j<num;j++){
schedule[i][j] = 0;
}
}
for(int i = 1;i < num+1;i++){
schedule[i-1][0] = i;
}
swap(schedule,k);
return schedule;
}
public static void swap(int [][]schedule,int k){
int times = 0;
while(times<k) {
int a = (int) Math.pow(2, times);
for (int i = 0; i < schedule.length; i++) {
for (int j = 0; j < a; j++) {
schedule[i][j + a] = schedule[i][j];
}
}
int kk = 0;
while (kk < schedule.length) {
for (int i = kk; i < kk + a; i++) {
for (int j = a; j < 2 * a; j++) {
int t = 0;
t = schedule[i][j];
schedule[i][j] = schedule[i + a][j];
schedule[i + a][j] = t;
}
}
kk += 2 * a;
}
times++;
}
}
}