At the moment, I came up with a sorting algorithm. I thought, "Isn't it going to work?", And when I wrote a little flow on a piece of paper around that area, there seems to be nothing strange about it. I implemented it with the glue of "Then, do you want to implement it! W".
Recently, due to lack of material, I have only introduced past programs, but this is one of them, and it is implemented in java. I think this was about a year ago.
The reason for this naming is that the move to achieve sorting using two stacks was similar to solving the Tower of Hanoi.
The following shows how to sort by Hanoi sort.
This sorting algorithm uses two stacks, a sorting stack and a temporary storage stack. The sort flow is shown below.
Please forgive me for the poor explanation. I think that the movement of the value is easy to understand if you can see the gif video above.
For the time being, I make full use of the stack and put in and out values while interposing comparisons. I'm not very confident in calculating the amount of calculation, but I think it's probably O (n ^ 2), so it's not an excellent sort. By the way, when comparing the sort time of 30,000 integers with the bubble sort, the processing time
Hanoi sort | Bubble sort |
---|---|
3.997s | 1.53s |
I ended up with a miserable result. Is the amount of calculation more than O (n ^ 2)?
I don't even know all the sorting algorithms. Maybe there is already a sort with the same mechanism, so if anyone knows it, please let me know!
hanoi_sort.java
import java.util.*;
import java.io.*;
class hanoi_sort{
static int[] data;
public static void main(String[] args) {
int dataNum = 30000;
data = new int[30000];
String[] strarray = new String[30000];
int k = 0;
try{
File file = new File("numbers.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String str= null;
k = 0;
while((str = br.readLine()) != null){
strarray[k]=str;
data[k] = Integer.parseInt(strarray[k]);
System.out.println("[" + k + "]" + data[k]);
k++;
}
br.close();
}catch(FileNotFoundException e){
System.out.println(e);
}catch(IOException e){
System.out.println(e);
}
for(int i = 0; i < dataNum; i++){
System.out.println("[" + i + "]: " + data[i]);
}
System.out.println("--------------sort---------------");
Deque<Integer> dequeFirst = new ArrayDeque<>();
Deque<Integer> dequeSecond = new ArrayDeque<>();
int box = 0;
int count = 0;
int countF = 0;
boolean flag = true;
dequeFirst.push(data[0]);
countF++;
double start = System.currentTimeMillis();
for(int i = 1; i < dataNum; i++){
flag = true;
while(flag){
box = dequeFirst.pop();
countF--;
if(box <= data[i]){
dequeFirst.push(box);
countF++;
dequeFirst.push(data[i]);
countF++;
for(int j = 0; j < count; j++){
dequeFirst.push(dequeSecond.pop());
countF++;
}
count = 0;
flag = false;
}else{
dequeSecond.push(box);
count ++;
if(countF == 0){
dequeFirst.push(data[i]);
countF++;
for(int j = 0; j < count; j++){
dequeFirst.push(dequeSecond.pop());
countF++;
}
count = 0;
flag = false;
}
}
}
}
double end = System.currentTimeMillis();
for(int i = 0; i < dataNum; i++){
System.out.println("[" + i + "]: " + dequeFirst.pop());
}
System.out.println("sort time: " + (end - start) + "ms");
System.out.println("sort time: " + (end - start)/1000 + "s");
}
}
By the way, the input is a text file, and by reading the one with an integer written on each line, the numerical value is stored in the array (everywhere fucking specification) Like this ↓
6418
18183
13643
6535
8436
3820
8969
19150
11902
4122
.
.
.
The programs and ideas of a year ago are embarrassing to look back on.
Until the end Thank you for reading!
Recommended Posts