Qsort in C

Поділитися
Вставка
  • Опубліковано 13 гру 2024

КОМЕНТАРІ • 67

  • @ironmonkey1990
    @ironmonkey1990 9 місяців тому +3

    Best video I’ve watched for this topic

  • @bonkerS605
    @bonkerS605 5 днів тому +1

    Straight up, very clean and easy to understand tutorial! Great explanation!

  • @rafaelpieri2771
    @rafaelpieri2771 Рік тому +3

    Not all heroes wear capes.

  • @andrew_mb
    @andrew_mb 4 роки тому +16

    This is exactly what I was looking for. You made qsort very easy to understand. Thank you.

  • @stefanbozic8880
    @stefanbozic8880 2 роки тому +5

    This video explains exactly what I was looking for with qsort and how it could be implemented for structures, thanks a bunch.

  • @shaboobis9283
    @shaboobis9283 2 роки тому +2

    Amazing lesson! So much information that is relevant to what I need to do. I'm very glad you provided a struct example. Bravo my friend.

  • @salvationwithoutregret9781
    @salvationwithoutregret9781 2 роки тому +1

    Superb explanation! Simple but strong. Im trying to make it through CS in College and this is a help!

  • @sahilkhan1909
    @sahilkhan1909 3 роки тому +1

    I absolutely like this video, this is exactly what I was looking for a couple of days.

  • @nullzero9224
    @nullzero9224 2 роки тому +1

    Well Explained and very nice tutorial. Thank you so much

  • @arpitanand451
    @arpitanand451 4 роки тому +4

    Past some days i am studing c language...
    And believe me no one is like you.
    Continue it sir.......

  • @anandraj9859
    @anandraj9859 2 роки тому +1

    Thanks for the beautiful explanation....... Thank u soooo much..

  • @md.jannatulnayem4328
    @md.jannatulnayem4328 3 роки тому +1

    Good content...absolutely loved it 🔥

  • @TheAbsoluteSir
    @TheAbsoluteSir 3 роки тому +1

    How are you utilizing *pa and *pb in the compareInt function if you are not passing anything in the arguments from main?

    • @programmingwithsikander3539
      @programmingwithsikander3539  3 роки тому +1

      ua-cam.com/video/lG-jfUiT02w/v-deo.html
      It's called from inside qsort function. The above link should help you.

  • @syedmufassiriqbal4473
    @syedmufassiriqbal4473 4 роки тому

    Very help full video , nyc explanation ✊🏻thank u sir .

  • @nathanoosterhuis6232
    @nathanoosterhuis6232 3 роки тому +1

    Thank you sir, good explanation

  • @uditmahajan555
    @uditmahajan555 4 роки тому +3

    Hi, it would be awesome if you add a section or even a photo of topics you'll cover in a video in your future videos! The video was nice (:

  • @harshitgupta424
    @harshitgupta424 3 роки тому +1

    thank you soo much ! great work there .

  • @ironironowski1063
    @ironironowski1063 4 роки тому +9

    HIT!! NIEWISKI GO N1ENAWIDZ1 ZNALAZŁ JEDEN PR0STY TRICK NA ZDANIE KOLOSA

    • @MISSONVAI
      @MISSONVAI 4 роки тому

      HIT !! THE SLAWS HIM HIM FOUND ONE SIMPLE TRICK ON THE COLLECT'S OPINION
      (your comment translation)

    • @ukaszkiepas57
      @ukaszkiepas57 4 роки тому

      xD

  • @Clara-cj2tu
    @Clara-cj2tu 3 роки тому +1

    Thank you so much!! This video is really helpful

  • @nbk330
    @nbk330 4 роки тому +1

    very easy way of explanation

  • @rhkhandu
    @rhkhandu 3 роки тому

    Would you please give the link of the PDF manual you followed during explanation the Syntex of qsort?

  • @asmakhan541
    @asmakhan541 4 роки тому

    Great explanation sir thank u😀😀

  • @MISSONVAI
    @MISSONVAI 4 роки тому +2

    sir, 'pa' and 'pb' are void pointers then how can we pass these two pointers to Strcmp() without typecasting them to char pointer?

    • @programmingwithsikander3539
      @programmingwithsikander3539  4 роки тому +5

      In C,
      We can assign address of any type to a void pointer without typecasting.
      Also the address stored in void pointer can be assigned to typed pointer.
      In C++ and in Turbo C, we require typecasting when we assign void pointer to typed pointer.
      To make it work, try this
      strcmp((char *)pa, (char *)pb);

  • @siraj1512
    @siraj1512 4 роки тому +1

    Very neat. Thank you

  • @Jack-ux3yh
    @Jack-ux3yh 4 роки тому +1

    very good video,learned a lot

  • @nml4546
    @nml4546 3 роки тому +1

    Very nice, thanks, I was thinking I had to write my own sort, and actually, but this is much better.

  • @tanvbd
    @tanvbd 3 роки тому

    Professor, I need to solve an array of pointers using qsort. I don't have enough time and I need to submit these projects within 2 weeks in my school. I honestly need help.

  • @sartrap1
    @sartrap1 4 роки тому +2

    thank you!!! really helpful

  • @Inublue50
    @Inublue50 3 роки тому +1

    Well taught!

  • @laurelanderson7572
    @laurelanderson7572 4 роки тому +2

    Thank you! I understand this now!

  • @tanvbd
    @tanvbd 3 роки тому

    Professor
    I am trying to qsort using name but its not happening.
    int cmpname(const void * pa, const void * pb ){
    const card_t *p1 = pa;
    const card_t *p2 = pb;
    // return p1->name - p2->name;
    return strcmp(p1->name, p2->name);
    }
    int n = sizeof(cards) / sizeof(card_t);
    qsort(cards, n, sizeof(card_t), cmpname);

    • @programmingwithsikander3539
      @programmingwithsikander3539  3 роки тому

      What is type of cards, is it multidimensional array or array of pointers?

    • @tanvbd
      @tanvbd 3 роки тому

      an array of pointers to structs

  • @wizz2681
    @wizz2681 4 роки тому

    I'm getting a segmentation fault when I do this, any suggestion?

  • @purabmodi6623
    @purabmodi6623 4 роки тому

    whats the value at address "pa" and "pb" ?

  • @azizprasetyo3676
    @azizprasetyo3676 2 роки тому +1

    Thank u sir😇

  • @kushbind8207
    @kushbind8207 5 років тому +1

    Thank you bro...😇😇😇😇

  • @vishalmitra_
    @vishalmitra_ 4 роки тому +2

    Wooow😊♨️
    I want to know reverse shotcut too

  • @qwert9313
    @qwert9313 4 роки тому +1

    Thank you so much ❤️

  • @ViralVideos-uz1mn
    @ViralVideos-uz1mn 5 років тому +1

    very helpful video

  • @karamveersachdeva935
    @karamveersachdeva935 4 роки тому +1

    really helpful

  • @nguyenphucquanganh7031
    @nguyenphucquanganh7031 3 роки тому +1

    thank you dude

  • @skitnado25
    @skitnado25 5 років тому +1

    Tried same thing but I kept getting id in something not a structure or union, mycode:
    /* QSORT and bsearch that returns a 24-bit color value using
    * built in library function
    */
    #include
    #include
    #include
    //declare color comparism
    int ColorCompare(const void *pa, const void *pb);
    //define struct for holding RGB color data
    typedef struct _RGB
    {
    int id;
    char color[16];
    int value;
    }RGB_t;
    int main(){
    int i, n;
    // record hold RGB color data
    RGB_t records [] = {{0,"black", 0x000000},
    {1,"blue", 0x0000AA},
    {2,"green", 0x00AA00},
    {3,"cyan", 0x00AAAA},
    {4,"red", 0xAA0000},
    {5,"magenta", 0xAA00AA},
    {6,"brown", 0xAA5500},
    {7,"light gray", 0xAAAAAA},
    {8,"dark gray", 0x555555},
    {9,"light blue", 0x5555FF},
    {10,"light green", 0x55FF55},
    {11,"light cyan", 0x55FFFF},
    {12,"light red", 0xFF5555},
    {13,"light magenta",0xFF55FF},
    {14,"yellow", 0xFFFF55},
    {15,"white", 0xFFFFFF}};

    n = sizeof(records) / sizeof(RGB_t);

    printf("List of colors in before sort records are:
    ");
    for(i = 0; i < n; i++){
    printf(" %d %s %x
    ", records.id[i], records.color[i], records.value[i]);
    printf("
    ");
    }

    qsort(records, n, sizeof(RGB_t),ColorCompare);
    printf("
    List of colors in afer sort records are:
    ");
    for(i = 0; i < n; i++){
    printf(" %d %s %x
    ", records.id[i], records.color[i], records.value[i]);
    printf("
    ");
    }
    return 0;
    }
    int ColorCompare(const void *pa, const void *pb){
    const RGB_t *p1 = pa;
    const RGB_t *p2 = pb;
    return strcmp(p1->color, p2->color);
    }

    • @programmingwithsikander3539
      @programmingwithsikander3539  5 років тому +3

      Records is an Array
      Use records[i].id, records[i].color etc.,
      #include
      #include
      #include
      //declare color comparism
      int ColorCompare(const void *pa, const void *pb);
      //define struct for holding RGB color data
      typedef struct _RGB
      {
      int id;
      char color[16];
      int value;
      }RGB_t;
      int main(){
      int i, n;
      // record hold RGB color data
      RGB_t records [] = {{0,"black", 0x000000},
      {1,"blue", 0x0000AA},
      {2,"green", 0x00AA00},
      {3,"cyan", 0x00AAAA},
      {4,"red", 0xAA0000},
      {5,"magenta", 0xAA00AA},
      {6,"brown", 0xAA5500},
      {7,"light gray", 0xAAAAAA},
      {8,"dark gray", 0x555555},
      {9,"light blue", 0x5555FF},
      {10,"light green", 0x55FF55},
      {11,"light cyan", 0x55FFFF},
      {12,"light red", 0xFF5555},
      {13,"light magenta",0xFF55FF},
      {14,"yellow", 0xFFFF55},
      {15,"white", 0xFFFFFF}};
      n = sizeof(records) / sizeof(RGB_t);
      printf("List of colors in before sort records are:
      ");
      for(i = 0; i < n; i++){
      printf(" %d %s %x
      ", records[i].id, records[i].color, records[i].value);
      printf("
      ");
      }
      qsort(records, n, sizeof(RGB_t),ColorCompare);
      printf("
      List of colors in afer sort records are:
      ");
      for(i = 0; i < n; i++){
      printf(" %d %s %x
      ", records[i].id, records[i].color, records[i].value);
      printf("
      ");
      }
      return 0;
      }
      int ColorCompare(const void *pa, const void *pb){
      const RGB_t *p1 = pa;
      const RGB_t *p2 = pb;
      return strcmp(p1->color, p2->color);
      }

    • @skitnado25
      @skitnado25 5 років тому +1

      @@programmingwithsikander3539 Thank you for the clarification it worked now .

    • @skitnado25
      @skitnado25 5 років тому

      so I added a bsearch to return the value os color from user input as shown below:
      for (; ;) {
      printf("
      Enter key like black or red for 24 bit color value:");
      /* Get the key, with size limit. */
      fgets(input,MAX_KEY_SIZE,stdin);
      /* Remove trailing newline, if there. and check for end of prog */
      if((strlen(input) == 1)) {
      printf("
      ****** end of program *********
      ");
      exit(0);
      }
      else {
      if ((strlen(input) > 0) && (input[strlen (input) - 1] == '
      ')){

      input[strlen (input) - 1] = '\0';
      printf("
      KEY entered is %s.
      ",input);
      RGB_t key = {.color = input};
      RGB_t const *RGB_ptr = bsearch(&key, records, n,
      sizeof (RGB_t), ColorCompare);
      if (RGB_ptr != NULL) {
      printf("Value for Color %s is %5x
      ", RGB_ptr->color, RGB_ptr->value);
      } else {
      printf("

      Color %s not found
      ", input);
      }
      }
      }
      }
      I get warnings:
      [Warning] initialization makes integer from pointer without a cast
      [Warning] (near initialization for 'key.color[0]')
      from the line of code:
      RGB_t key = {.color = input};
      when I entered:
      RGB_t key = {.color = "red"};
      i get no error and it returns value from records.

    • @programmingwithsikander3539
      @programmingwithsikander3539  5 років тому +1

      @@skitnado25
      Replace
      RGB_t key = {.color = input};
      with
      RGB_t key;
      strcpy(key.color , input);

  • @GenieChef
    @GenieChef 4 роки тому

    Dude you are my hero! Thank you!

  • @ohmnamo7056
    @ohmnamo7056 4 роки тому

    bro how does pa and pb gets the address of the 2 elements in the array? thanku

    • @programmingwithsikander3539
      @programmingwithsikander3539  4 роки тому +2

      Watch this video
      ua-cam.com/video/lG-jfUiT02w/v-deo.html
      Compare function is called within the qsort function.

    • @ohmnamo7056
      @ohmnamo7056 4 роки тому

      @@programmingwithsikander3539 thankss a lottt brother ..you made me understood quickly .Very understandable explanation👏👏

  • @bashmogd4468
    @bashmogd4468 4 роки тому +1

    thanx very much

  • @surjeetsingh-tj7di
    @surjeetsingh-tj7di 4 роки тому

    Awsome bro

  • @orangeshoes
    @orangeshoes 4 роки тому

    Thank you ❤️

  • @rathnaganesh595
    @rathnaganesh595 4 роки тому +1

    great !!! Thank you :)

  • @anmolraj6604
    @anmolraj6604 4 роки тому

    Well taught

  • @axolet
    @axolet 4 роки тому

    Thank you!

  • @Zambo23
    @Zambo23 4 роки тому +1

    so clear, ty

  • @Fariiis
    @Fariiis 4 роки тому

    please work on your accent

    • @pratiks235
      @pratiks235 2 роки тому +1

      what does his accent have to do with the knowledge imparted? i found it pretty easy to understand