Two interesting algorithms recently seen: Sleep Sort and coin Simulation

Source: Internet
Author: User

Sleep Sort

SleepSort is a sorting algorithm that uses multiple threads for different sleep times. First look at the original version

#!/bin/bash
function f() {
sleep "$1"
echo "$1"
}
while [ -n "$1" ]
do
f "$1" &
shift
done
wait ./sleepsort.bash 5 3 6 3 6 3 1 4 7

Pretty awesome. However, it seems difficult to understand Shell scripts. For example, I --!. In fact, the main idea of this algorithm is to use the number to be sorted as the thread sleep time and then output it in sequence. I made a simple implementation using Java.

package Test;

public class SleepSort extends Thread {

public int temp;

SleepSort(int para)
{
this.temp=para;
}

public void run() {
try {
Thread.sleep(temp);
System.out.println(temp);
} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
try {
int[] a = { 1, 1, 7, 2, 9 };
SleepSort[] test=new SleepSort[a.length];
for (int i = 0; i < a.length; i++) {
test[i]=new SleepSort(a[i]);
test[i].start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

However, Sleep Sort cannot Sort negative numbers and floating-point numbers. Of course, for negative numbers, you can add a huge number to change to a positive number. But it must be slow. In addition, if the system thread sleep precision is insufficient, the sorting speed is certainly inferior to those of the traditional sorting methods.

Coin Simulation

Problem description: The probability of a coin going up and down is not equal. How can I use this coin to simulate a coin with an equal probability. A little abstract. There is a random function f outputs 1, 1-p probability output 0 with the probability of p. How to use this function to construct a function that outputs 1 with the probability of 0.5. Some cool people in a group gave a better answer:

 int func()
{
int i ;
int j ;
while(true)
{
i = f() ;
j = f() ;
if(i == 1 && j == 0)
return 1;
else if(i == 0 && j == 1)
return 0;
}
}

This code is equivalent to throwing a coin twice and combining it into 11, 01, 10, 00. Among them, 10 and 01 are equivalent to p (1-p) and (1-p) p respectively. The two probabilities are equal. Therefore, you only need to output 1 and 0 for both cases. In other cases, the output is not output and the loop is still executed.

In the circular body, some people give a more concise way of writing.

if(i!=j)
return i;

Of course, some people have raised further questions. What if the probability output 1 of 30% is constructed. Simple consideration. If you add a coin, there will be 2 ^ 3 = 8 types of coins, of which 000 | 001 | 010 | 011 | 110 | 100 | 101 | 111 |. among them, there are three types of one, 001 | 010 | 100. in this case, can we construct an output with a probability of 1/3 and 2/3 respectively based on the above ideas. According to this idea, if there are 10 kinds of situations where only one coin occurs, then we can construct a program with an output of 30% as 1.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.