Convert CocktailSort algorithm from Java to C ++

0
public class CocktailSort {

    public static int izquierda, derecha, ultimo;
    public static int arreglo[] = {10, 23, 6, 4, 223, 2, 112, 3, 6, 34};

    public static void main(String[] args) {

        izquierda = 1;
        derecha = arreglo.length;
        ultimo = arreglo.length - 1;

        do {
            for (int i = arreglo.length - 1; i > 0; i--) {
                if (arreglo[i - 1] > arreglo[i]) {
                    int aux = arreglo[i];
                    arreglo[i] = arreglo[i - 1];
                    arreglo[i - 1] = aux;
                    ultimo = i;
                }
            }

            izquierda = ultimo + 1;

            for (int j = 1; j < arreglo.length; j++) {
                if (arreglo[j - 1] > arreglo[j]) {
                    int aux = arreglo[j];
                    arreglo[j] = arreglo[j - 1];
                    arreglo[j - 1] = aux;
                    ultimo = j;
                }
            }
            derecha = ultimo - 1;
        } while (derecha >= izquierda);

        for (int i = 0; i < arreglo.length; i++) {
            System.out.println(arreglo[i]);
        }
    }
}
    
asked by Tecprogramer 09.04.2017 в 07:10
source

1 answer

1

This is the code in C ++. I am not a C ++ programmer, so I may have errors or bad practices:

#define LENGHT 10

#include <iostream>

using namespace std;

int izquierda, derecha, ultimo;
int arreglo[LENGHT] = { 10, 23, 6, 4, 223, 2, 112, 3, 6, 34 };

int main() {

    izquierda = 1;
    derecha = LENGHT;
    ultimo = LENGHT - 1;

    do {
        for (int i = LENGHT - 1; i > 0; i--) {
            if (arreglo[i - 1] > arreglo[i]) {
                int aux = arreglo[i];
                arreglo[i] = arreglo[i - 1];
                arreglo[i - 1] = aux;
                ultimo = i;
            }
        }

        izquierda = ultimo + 1;

        for (int j = 1; j < LENGHT; j++) {
            if (arreglo[j - 1] > arreglo[j]) {
                int aux = arreglo[j];
                arreglo[j] = arreglo[j - 1];
                arreglo[j - 1] = aux;
                ultimo = j;
            }
        }
        derecha = ultimo - 1;
    } while (derecha >= izquierda);

    for (int i = 0; i < LENGHT; i++) {
        cout << arreglo[i] << endl;
    }

    // pausa
    cin.get();
    return 0;
}
    
answered by 09.04.2017 / 07:46
source