mercredi 1 février 2017

Bubble sort 2d array in alphabetical order only using iostream?

I'm trying to figure out how I can sort a list of words in alphabetical order. The code I have now will sort them correctly, only if each word is only 1 letter. Once I move to more letters, it fails. Can you help me fix this. Thanks

My input file is: Emma Liam Dill Bill Kimm Jacks John Hats Julia Jill

#include <iostream>
#include <fstream>
using namespace std;

void sort(char input[10][25], int size);

int main(){
        char input[10][25] = {"Emma", "Liam", "Dill", "Bill", "Kimm", "Jacks", "John" "Hats", "Julia", "Jill"};
        
        sort(input,10);
        
        cout << "Sorted:\n";
        for(int i = 0; i < 10; i++){
                cout << input[i];
        }
        
        return 0;
}


void sort(char input[10][25],int size){
        
        char temp;
        for(int k = 0;k < 10;k++){
                for(int i = 0; i < 9; i++){
                        for(int j = 0; j < 24; j++){
                                if(input[i][j] > input[i+1][j]){
                                        temp = input[i][j];
                                        input[i][j] = input[i+1][j];
                                        input[i+1][j] = temp;
                                }
                        }
                }
        }
        
}

Aucun commentaire:

Enregistrer un commentaire