[NEFU sharp data structure] operation related to experiment 8 sorting table

Keywords: data structure

[NEFU sharp data structure] operation related to experiment 8 sorting table

Serious people who handwritten sort ah, brainless sort.
If you are admitted to the computer sorting stage, sort will kill indiscriminately, but it's a pity that you can't arrange it.
Recommended reading: [data structure] NEFU sophomore sharp experiment reference directory

Calf pulling Time

This should be the end of the Ruige data structure series. We have basically learned the sorting algorithm, so let's chat. If you just look at the answer, skip it directly, which may affect your mood of the day awa.

About questions:
Before asking something, I went to the search engine and tried my own solution is the basic requirement. It's a waste of other people's time to search some things and bother others again. If you bother others without making efforts, then I will consider the following two situations: you are a lazy dog or your focus is on communication rather than solving problems.

My personal solution to these situations depends on my time, my mood and the weight of the other party in my heart.
If I don't know you and you ask a very simple question, I will probably ignore you.

About address:
Personally, I don't like what people call me, whether you praise me or make fun of me. I don't have any deep interpersonal relationship. Boring titles will only deepen the estrangement between you and me and make me feel lonely. Acquaintances don't need to be called to speak directly. Strangers can match their classmates.

About stage test:
If you have your own planning and preparation, it may be your obstacle. It's over. (except the students of Chong Baoyan)
If you don't have your own plan, the test will help you find out the bottom at that stage. Without this bottom, you may become very bad. You can learn everything with peace of mind.

subject

8550

Heap sorting, too lazy to write the heap, directly the priority of C++ STL_ Queue priority queue can be regarded as a heap, which literally means a queue with order. It must have been arranged in order

#include<iostream>
#include<queue>
#include<vector>
using namespace std;

int main(){
    priority_queue<int,vector<int>,greater<int>>heap;
    int x;while(cin>>x&&x)heap.push(x);
    while(!heap.empty()){cout<<heap.top()<<" ",heap.pop();}
    return 0;
}

8549

Classic fast platoon. It's very useful to find a job. I won't memorize the suggestions

#include<algorithm>
#include<cstdio>

using namespace std;

const int N=1e5+5;
int q[N];
void quick_sort(int q[],int l,int r)
{
    if(l>=r)return;
    int i=l-1,j=r+1,x=q[l+r+1>>1];
    while(i<j)
    {
        do i++;while(q[i]<x);
        do j--;while(q[j]>x);
        if(i<j)swap(q[i],q[j]);
    }
    quick_sort(q,l,i - 1),quick_sort(q,i,r);
}

int main()
{
    int n=0,x;
    while(~scanf("%d",&x)&&x)q[++n]=x;
    quick_sort(q,1,n);
    for(int i=1;i<=n;i++)printf("%d ",q[i]);
}

8548

Simple selection sort

#include <stdio.h>
void Sort(int a[],int n){
    for(int i=0; i<n-1; i++){
        int min_index = i; 
        for(int j=i+1; j<n; j++)
            if(a[j] < a[min_index])
                min_index = j;
        
        if( i != min_index){
            int temp = a[i];
            a[i] = a[min_index];
            a[min_index] = temp;
        }
    }
}

int A[105];
int n,x;
int main() {
	while(~scanf("%d",&x)&&x)A[n++]=x;
	Sort(A,n);
	for(int i=0;i<n;i++)printf("%d ",A[i]);
	return 0;
}

8547

Hill sort, optimized insertion sort

#include <stdio.h>
void Sort(int a[], int len){
    int i, j, k, tmp, gap;  // gap is the step size
    for (gap = len / 2; gap > 0; gap /= 2) {  // The step size is initialized to half the length of the array. After each traversal, the step size is halved,
    	for (i = 0; i < gap; ++i) { // Variable i is the index of the first element of each grouping 
	        for (j = i + gap; j < len; j += gap) { //The elements with a step size of gap are sorted in-line. When gap is 1, it is sorted in-line
	            tmp = a[j];  // Value of backup a[j]
	            k = j - gap;  // j is initialized to the previous element of i (gap length difference from i)
	            while (k >= 0 && a[k] > tmp) {
	                a[k + gap] = a[k]; // Move the element before a[i] and larger than the value of tmp one bit backward
	                k -= gap;
	            }
	            a[k + gap] = tmp; 
	        }
	    }
    }
}


int A[105];
int n,x;
int main() {
	while(~scanf("%d",&x)&&x)A[n++]=x;
	Sort(A,n);
	for(int i=0;i<n;i++)printf("%d ",A[i]);
	return 0;
}

8546

Bubble sorting

#include <stdio.h>
void Sort(int arr[], int n) {
    int temp;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n - i - 1; ++j) {
            if (arr[j] > arr[j + 1]) {
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}
int A[105];
int n,x;
int main() {
	while(~scanf("%d",&x)&&x)A[n++]=x;
	Sort(A,n);
	for(int i=0;i<n;i++)printf("%d ",A[i]);
	return 0;
}

8545

Direct insert sort

#include <stdio.h>
int InsertSort(int A[], int n) {
	for(int i=2; i<=n; i++) { 
		A[0]=A[i];
        int j; 
		for(j=i-1; A[0]<A[j]; j--)A[j+1]=A[j];
		A[j+1]=A[0];
	}
	return 0;
}
int A[105];
int n,x;
int main() {
	while(~scanf("%d",&x)&&x)A[++n]=x;
	InsertSort(A,n);
	for(int i=1;i<=n;i++)printf("%d ",A[i]);
	return 0;
}

Posted by patryn on Wed, 03 Nov 2021 11:54:59 -0700