[PYTHON] I tried to program bubble sort by language

I was wondering what the difference in grammar would look like if I programmed with the same subject, so I created a simple program so that I could compare it. The description method is different for each language, but the common control statement for for and if is used, and the replacement process is the same. I realized once again that I thought language was a tool for system development.

PHP

$dt = array(7,5,9,6,2,4,1,3,8);
for($i = 0; count($dt) > $i; $i++){
    foreach($dt as $k=>$v){
        $set_k = $k + 1;
        if(isset($dt[$set_k]) && $v > $dt[$set_k]){
            $tmp = $dt[$set_k];
            $dt[$set_k] = $dt[$k];
            $dt[$k] = $tmp;
        }
    }
}

Java

int dt[] = {7,5,9,6,2,4,1,3,8};
for(int i = 0;dt.length - 1 > i;i++){
    for(int j = 0;dt.length - 1 > j;j++){
        int k = j + 1;
        if(dt[j] > dt[k]){
            int tmp = dt[k];
            dt[k] = dt[j];
            dt[j] = tmp;
        }
    }
}

JavaScript

let dt = [7,5,9,6,2,4,1,3,8];
for(let i = 0;dt.length - 1 > i;i++){
    for(let j = 0;dt.length - 1 > j;j++){
        let k = j + 1;
        if(dt[j] > dt[k]){
            let tmp = dt[k];
            dt[k] = dt[j];
            dt[j] = tmp;
        }
    }
}

Python

dt = [7,5,9,6,2,4,1,3,8]
for i in dt:
    for j in dt:
        k = dt.index(j) + 1
        if len(dt) > k and j > dt[k]:
            tmp = dt[k]
            dt[k] = j
            dt[dt.index(j)] = tmp

Ruby

dt = [7,5,9,6,2,4,1,3,8]
for a in dt do
    dt.map.with_index{|v,i|
        k = i + 1
        if !dt[k].nil? && v > dt[k]
            tmp = dt[k]
            dt[k] = v
            dt[i] = tmp
        end
    }
end

Recommended Posts

I tried to program bubble sort by language
I tried to sort a random FizzBuzz column with bubble sort.
I tried to get an image by scraping
I tried to debug.
I tried to paste
[Python] Try to make a sort program by yourself. (Selection sort, insertion sort, bubble sort)
I tried to implement selection sort in python
I tried to classify dragon ball by adaline
I tried to identify the language using CNN + Melspectogram
I tried to learn PredNet
I tried to organize SVM.
I tried to implement PCANet
I tried to reintroduce Linux
I tried to introduce Pylint
I tried 100 language processing knock 2020
I tried to summarize SparseMatrix
I tried to touch jupyter
I tried to implement StarGAN (1)
I tried to aggregate & compare unit price data by language with Real Gachi by Python
I tried to implement anomaly detection by sparse structure learning
I tried to speed up video creation by parallel processing
[Introduction to simulation] I tried playing by simulating corona infection ♬
[Django] I tried to implement access control by class inheritance.
[Introduction to Pandas] I tried to increase exchange data by data interpolation ♬
I tried to illustrate the time and time in C language
I tried to classify MNIST by GNN (with PyTorch geometric)
I tried to implement Harry Potter sort hat with CNN
I tried to divide with a deep learning language model
I tried to implement Deep VQE
I tried to create Quip API
I tried to touch Python (installation)
I tried 100 language processing knock 2020: Chapter 3
I tried to implement adversarial validation
I tried to explain Pytorch dataset
I tried Watson Speech to Text
I tried to touch Tesla's API
I tried to implement hierarchical clustering
I tried to organize about MCMC.
I tried 100 language processing knock 2020: Chapter 1
I tried to implement Realness GAN
I tried to move the ball
I tried 100 language processing knock 2020: Chapter 2
I tried 100 language processing knock 2020: Chapter 4
I tried to estimate the interval.
I tried to create a simple credit score by logistic regression.
[Introduction to simulation] I tried playing by simulating corona infection ♬ Part 2
I tried to solve 100 language processing knock 2020 version [Chapter 2: UNIX commands 10 to 14]
I tried to visualize the Beverage Preference Dataset by tensor decomposition.
I tried to implement sentence classification by Self Attention with PyTorch
I tried to summarize the commands used by beginner engineers today
I tried to predict by letting RNN learn the sine wave
I tried to create Bulls and Cows with a shell program
I tried to visualize Boeing of violin performance by pose estimation
I tried to solve 100 language processing knock 2020 version [Chapter 2: UNIX commands 15 to 19]
I tried to solve the shift scheduling problem by various methods
I tried to create a linebot (implementation)
I tried to summarize Python exception handling
I tried to implement PLSA in Python
I tried using Azure Speech to Text.
I tried moving the image to the specified folder by right-clicking and left-clicking
I tried to implement Autoencoder with TensorFlow