0

I have an array of a Struct and trying to sort them alphabetically by lname ,i tried bubble sort but when we have some empty items in our array bubble sort won't work correctly .so is there any function to sort these items ?

This is my code :

#include<iostream>
#include<algorithm>

using namespace std;


struct user {

char lname[30];
int userid;
};

user libuser[1000];

int main(){


strcpy(libuser[0].lname,"");
libuser[0].userid = 0;


strcpy(libuser[1].lname,"backzade");
libuser[1].userid = 1;

strcpy(libuser[2].lname,"akhondali");
libuser[2].userid = 2;

    strcpy(libuser[3].lname,"sayidian");
libuser[3].userid = 3;

    strcpy(libuser[4].lname,"navah");
libuser[4].userid = 4;

    strcpy(libuser[5].lname,"mostarab");
libuser[5].userid = 5;


libuser[6].userid = 0;


    strcpy(libuser[7].lname,"");
libuser[7].userid = 0;

    strcpy(libuser[8].lname,"");
libuser[8].userid = 0;

    strcpy(libuser[9].lname,"borobaba");
libuser[9].userid = 9;

    strcpy(libuser[10].lname,"divune");
libuser[10].userid = 10;



for(int i=1;i<1000;i++)
    if(libuser[i].userid!=0)
        cout<<libuser[i].lname<<"\n";


system("PAUSE");

return 0;
}
6
  • You need to some way to tell which elements of the array are in use. Like a counter variable or initialize all the unused elements to userid=0. Commented Jan 1, 2014 at 5:04
  • 5
    @Barmar: qsort()? Why not use std::sort()? Commented Jan 1, 2014 at 5:17
  • Would this be considered a duplicate? The basic approach is that you use pointers for the beginning and end of the array as well as a special function that does the actual comparison. Have you considered this approach? Commented Jan 1, 2014 at 5:20
  • @DietmarKühl Because I know C better than I know C++ :) Commented Jan 1, 2014 at 5:22
  • Why doesn't bubble sort work correctly? You obviously need to define what you want it to do about empty elements, but that's true of any sorting method. Also note that libuser[6] is currently not empty, but undefined. Commented Jan 1, 2014 at 6:18

2 Answers 2

5

This code just can be one method among many good methods. I just use std::sort and lambda. I hope this will help you a little. Happy new year!

std::sort (std::begin(libuser), std::end(libuser), [&](const user& first, const user& second) -> bool
{
    return (first.lname[0] < second.lname[0]); 
});
Sign up to request clarification or add additional context in comments.

Comments

1
#include <iostream>
#include <algorithm>

using namespace std;

struct user
{
    char lname[30];
    int userid;
};

user libuser[1000];
int elementCount = 0;

int main()
{
    strcpy(libuser[0].lname,"");
    libuser[0].userid = 0;

    strcpy(libuser[1].lname,"backzade");
    libuser[1].userid = 1;

    strcpy(libuser[2].lname,"akhondali");
    libuser[2].userid = 2;

    strcpy(libuser[3].lname,"sayidian");
    libuser[3].userid = 3;

    strcpy(libuser[4].lname,"navah");
    libuser[4].userid = 4;

    strcpy(libuser[5].lname,"mostarab");
    libuser[5].userid = 5;

    libuser[6].userid = 0;

    strcpy(libuser[7].lname,"");
    libuser[7].userid = 0;

    strcpy(libuser[8].lname,"");
    libuser[8].userid = 0;

    strcpy(libuser[9].lname,"borobaba");
    libuser[9].userid = 9;

    strcpy(libuser[10].lname,"divune");
    libuser[10].userid = 10;

    //sort
    for(int i = 0; i < 1000; i++)
    {
        for(int j = 0; j < 1000 - 1; j++)
        {
            if(strcmp(libuser[j].lname, libuser[j + 1].lname) > 0)  //change to < 0 for descending sort
            {
                user temp = libuser[j];
                libuser[j] = libuser[j + 1];
                libuser[j + 1] = temp;
            }
        }
    }


    for(int i = 1; i < 1000; i++)
    {
        if(libuser[i].userid!=0)
        {
            cout<<libuser[i].lname<<"\n";
        }
    }


    system("PAUSE");

    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.