Interesting sorting algorithm-detailed introduction to Monkey King sorting

Preface

Sorting algorithms are often used in problems. In the program, we generally use fast sorting, merge, heap sorting and other high-efficiency sorting, and even more directly use sort sorting. Today, I want to introduce a peculiar Sorting method- Monkey King sorting , I believe the following content will be helpful to you.

Introduction to Algorithm

Monkey King sorting, also known as Gigi King sorting, is a highly efficient sorting algorithm. Its inventor is LEMT Konjac. It was invented on January 28, 2021 when it was paddling in the computer room. To be optimized), this algorithm has a very low learning threshold and is suitable for beginners to learn.

thought

To simulate a monkey (bushi),
we imagine to take two variables xxxyyy , represents the two numbers we may want to swap,swap (a [x], a [y]) swap(a[x],a[y])swap(a[x],a [ y ] ) , after exchangeO (n) O(n)O ( n ) Scan to determine whether the current sequence is legal.

At this time, someone may ask: Isn't the time complexity high?

indeed!

So the following is the core part of King Gigi's order!

It is also a key part of reducing time complexity!

We can imagine how to reduce the enum xxx and yy time. It is not difficult to think that we can imitate a monkey and choose two numbers randomly and exchange them directly. (Yi is serious)

CODE is as follows

//This is monkeykingsort ! ! !
#include<bits/stdc++.h>
using namespace std;
int n,a[1000005],bj;
int main() {
    
    
	scanf("%d",&n);
	for (int i=1;i<=n;i++) scanf("%d",&a[i]);
	for (int i=2;i<=n;i++) if (a[i]<a[i-1]) {
    
    bj=1; break;}
	srand(time(NULL));
	while (bj) {
    
    
		int x=rand()%n+1,y=rand()%n+1;
		swap(a[x],a[y]),bj=0;
		for (int i=2;i<=n;i++) if (a[i]<a[i-1]) {
    
    bj=1; break;}
	}
	for (int i=1;i<=n;i++) printf("%d\n",a[i]);
}

But what if the face is dark? ? ?
It is a pity that you are not the real King Gigi, but we can turn you into the real King Gigi through optimization (???)

optimization

When we enumerate two numbers, we can think about the circumstances under which we can exchange.

Obviously, when x <y x <yx<y a [ x ] > a [ y ] a[x]>a[y] a[x]>a [ y ] situation we can exchange (from small to large row)

So the optimized code is as follows:

//This is monkeykingsort ! ! !
#include<bits/stdc++.h>
using namespace std;
int n,a[1000005],bj;
int main() {
    
    
	scanf("%d",&n);
	for (int i=1;i<=n;i++) scanf("%d",&a[i]);
	for (int i=2;i<=n;i++) if (a[i]<a[i-1]) {
    
    bj=1; break;}
	srand(time(NULL));
	while (bj) {
    
    
		int x=rand()%n+1,y=rand()%n+1;
		if (x>y) swap(x,y);
		if (a[x]>a[y]) {
    
    
			swap(a[x],a[y]),bj=0;
			for (int i=2;i<=n;i++) if (a[i]<a[i-1]) {
    
    bj=1; break;}
		}
	}
	for (int i=1;i<=n;i++) printf("%d\n",a[i]);
}

I think it's okay ~~~

time complexity

Theory O (TN) O(TN)O ( T N ) (T is the number of exchanges, "constant" and can be omitted) Provided that theface is good enough? ? ?

So it will degenerate to O (infinite) O (infinite)O ( no limit ) ? ? ?

So be it. . .

No one really sees here, right (escape

Guess you like

Origin blog.csdn.net/qq_49972640/article/details/113358910