Προς το περιεχόμενο

Selection Sort algorithm review (c++)


Προτεινόμενες αναρτήσεις

Δημοσ.

Θα μπορούσε να μου κάνει κάποιος review/αξιολόγηση τον παρακάτω κώδικα;

είναι ο αλγόριθμος ταξινόμησης επιλογής, υλοποιημένος σε c++ .

 

 

 

Ευχαριστώ,

//practice//
#include <iostream>
#include <cstdlib>

using namespace std;
void selectionsort(int *myarray, int length) //συνάρτηση ταξινόμησης
{
    int mmin,i,j;//το mmin είναι η θέση του ελαχίστου στον πίνακα myarray.
    for(i=0;i<(length-1);i++)
    {
        mmin=i+1;
        for(j=i;j<length;j++)
        {
            if(myarray[j]<myarray[mmin])
                mmin=j;
        }
        int temp;
        temp=myarray[i];
        myarray[i]=myarray[mmin];
        myarray[mmin]=temp;
    }
}
void printing(int *myarray,int length)
{
    int i;
    for(i=0;i<length;i++)
    {
        cout<<myarray[i]<<" ";
        cout<<"\n";
    }
    cout<<"\n";
}
int main()
{
    char choice;
    srand(time(NULL));
    cout << "Hello world!" << endl;
    cout << "This is the selection sort algorithm!"<<endl;
    cout<<"Would you like to give the elements of the array? (y/n)"<<endl;
    cin>>choice;
    cout<<"\n";
    if(choice=='y')
    {
        int numbers;
        cout<<"How many elements do you want in the array?"<<endl;
        cin>>numbers;
        if(numbers==1)
        {
            cout<<"I can not sort only one number!\nExiting..."<<endl;
            exit(0);
        }
        int *myarray;
        myarray = new int [numbers];
        cout<<"So give the elements one by one!"<<endl;
        int i;
        for(i=0;i<numbers;i++)
        {
            cin>>myarray[i];
        }
        cout<<"The elements you gave are :"<<endl;
        /*for(i=0;i<numbers;i++)
            cout<<myarray[i]<<" "<<endl;
        cout<<"\n";*/
        printing(myarray,numbers);
        //selection sorting
        selectionsort(myarray,numbers);
        //printing
        cout<<"The elements are sorted..."<<endl;
        printing(myarray,numbers);
        delete[] myarray;
    }else
    {
        if(choice=='n')
        {
            int numbers;
            int i;
            cout << "Ok i will randomly fill the array!\nBut tell me how many elements you want..."<<endl;
            cin >> numbers;
            if(numbers==1)
            {
                cout<<"I can not sort only one number!\n(Superman can't)\nExiting..."<<endl;
                exit(0);
            }
            int *myarray;
            myarray = new int [numbers];
            for(i=0;i<numbers;i++)
            {
                myarray[i]=rand();
            }
            cout<<"The elements you gave are :"<<endl;
            printing(myarray,numbers);
            /*for(i=0;i<numbers;i++)
                cout<<myarray[i]<<" "<<endl;
            cout<<"\n";*/
            //selection sort
            selectionsort(myarray,numbers);
            //printing
            cout<<"The elements are sorted..."<<endl;
            printing(myarray,numbers);
            delete[] myarray;
        }else
            cout<<"You did not press neither y nor n.\nExiting..."<<endl;
    }
    return 0;
}

Δημοσ.

 

 

Για πρώτη μάτια που τον βλέπω είναι ένταξη στο θέμα sorting, θα πρότεινα να παίξεις με switch και όχι if/else στο choice.

Και τα int length καν τα const int για να είναι σταθερά.

Δημοσ.

Ο κώδικας δεν έχει κάποιο λάθος. Δες και τον δικό μου,

#include <iostream>
#include <array>
#include <algorithm>

void ssort(int *m, int len)
{
  for(int i = 0; i < len; i++) {
    int j = std::min_element(m + i, m + len) - m;
    std::swap(m[i], m[j]);
  }
}

int main(void)
{
  int m[] = { 3, 4, 2, 5, 1, 6, 4, 2, 3, 7, 10 };
  for(auto x : m) std::cout << x << " ";
  std::cout << std::endl;
  ssort(m, sizeof m / sizeof *m);
  for(auto x : m) std::cout << x << " ";
  std::cout << std::endl;
  return 0;
}

Μπορείς και να μην ζητάς το μέγεθος του πίνακα, απλώς να διαβάζεις αριθμούς μέχρι αποτυχίας.


Για πρώτη μάτια που τον βλέπω είναι ένταξη στο θέμα sorting, θα πρότεινα να παίξεις με switch και όχι if/else στο choice.

Και τα int length καν τα const int για να είναι σταθερά.

Το length δεν πρέπει να είναι const. Δεν υπάρχει κάποια λογική σε αυτό. Είναι παράμετρος (argument) σε συνάρτηση, το αν θα τροποποιηθεί ή όχι δεν ενδιαφέρει τον χρήστη (και φυσικά δεν βοηθάει τον compiler).

Δημιουργήστε ένα λογαριασμό ή συνδεθείτε για να σχολιάσετε

Πρέπει να είστε μέλος για να αφήσετε σχόλιο

Δημιουργία λογαριασμού

Εγγραφείτε με νέο λογαριασμό στην κοινότητα μας. Είναι πανεύκολο!

Δημιουργία νέου λογαριασμού

Σύνδεση

Έχετε ήδη λογαριασμό; Συνδεθείτε εδώ.

Συνδεθείτε τώρα
  • Δημιουργία νέου...