... 1576 241833 66 Built into qsort is a function that can swap two given array elements.

26
... 15 76 24 18 33 66 into qsort is a function that can swap two elements.
  • date post

    22-Dec-2015
  • Category

    Documents

  • view

    214
  • download

    0

Transcript of ... 1576 241833 66 Built into qsort is a function that can swap two given array elements.

Page 1: ... 1576 241833 66 Built into qsort is a function that can swap two given array elements.

...15 76 24 18 33 66

Built into qsort is a function that can swap two given array elements.

Page 2: ... 1576 241833 66 Built into qsort is a function that can swap two given array elements.

...15 76 24 18 33 66

. elem_1 . elem_2

Built into qsort is a function that can swap two given array elements.

Page 3: ... 1576 241833 66 Built into qsort is a function that can swap two given array elements.

...15 33 24 18 76 66

. elem_1 . elem_2

Built into qsort is a function that can swap two given array elements.

Page 4: ... 1576 241833 66 Built into qsort is a function that can swap two given array elements.

...15 33 24 18 76 66

Built into qsort is a function that can swap two given array elements. Because qsort is general (can operate on any array), there needs to be some function that takes references to any pair of array elements as input and determines which is smaller.

Page 5: ... 1576 241833 66 Built into qsort is a function that can swap two given array elements.

...15 33 24 18 76 66

Built into qsort is a function that can swap two given array elements. Because qsort is general (can operate on any array), there needs to be some function that takes references to any pair of array elements as input and determines which is smaller.

What general mechanism does c++ provide for passing objects to functions?

Page 6: ... 1576 241833 66 Built into qsort is a function that can swap two given array elements.

...15 33 24 18 76 66

Built into qsort is a function that can swap two given array elements. Because qsort is general (can operate on any array), there needs to be some function that takes references to any pair of array elements as input and determines which is smaller.

What general mechanism does c++ provide for passing objects to functions? Answer: void *

Page 7: ... 1576 241833 66 Built into qsort is a function that can swap two given array elements.

...15 33 24 18 76 66

Suppose array elements are of type int. How do you find the value of the elements?

Page 8: ... 1576 241833 66 Built into qsort is a function that can swap two given array elements.

...15 33 24 18 76 66

. elem_1 . elem_2

Suppose array elements are of type int. How do you find the value of the elements? Answer: int cmp(void *elem_1, void *elem_2) {

Page 9: ... 1576 241833 66 Built into qsort is a function that can swap two given array elements.

...15 33 24 18 76 66

. elem_1 . elem_2

Suppose array elements are of type int. How do you find the value of the elements? Answer: int cmp(void *elem_1, void *elem_2) { int val_1 = *elem_1; int val_2 = *elem_2;

Page 10: ... 1576 241833 66 Built into qsort is a function that can swap two given array elements.

...15 33 24 18 76 66

. elem_1 . elem_2

Suppose array elements are of type int. How do you find the value of the elements? Answer: int cmp(void *elem_1, void *elem_2) { int val_1 = *elem_1; int val_2 = *elem_2;

No Good! Cannot find value of unknown element type

Page 11: ... 1576 241833 66 Built into qsort is a function that can swap two given array elements.

...15 33 24 18 76 66

. elem_1 . elem_2

Suppose array elements are of type int. How do you find the value of the elements? Answer: int cmp(void *elem_1, void *elem_2) { int val_1 = *(int *)elem_1; int val_2 = *(int *)elem_2;

That's it!! Cast the pointer to be a pointer to type int

Page 12: ... 1576 241833 66 Built into qsort is a function that can swap two given array elements.

...15 33 24 18 76 66

. elem_1 . elem_2

Suppose array elements are of type int. How do you find the value of the elements? Answer: int cmp(void *elem_1, void *elem_2) { int val_1 = *(int *)elem_1; int val_2 = *(int *)elem_2; if (val_1 < val_2) return -1; if (val_1 > val_2) return 1; return 0; }

Page 13: ... 1576 241833 66 Built into qsort is a function that can swap two given array elements.

...15 2 1

33 3 9

24 1 0

18 5 6

76 2 3

66 1 9

. elem_1 . elem_2

typedef struct { int city_1, city_1, cost } Cable; int cmp(void *elem_1, void *elem_2) {

Page 14: ... 1576 241833 66 Built into qsort is a function that can swap two given array elements.

...15 2 1

33 3 9

24 1 0

18 5 6

76 2 3

66 1 9

. elem_1 . elem_2

typedef struct { int city_1, city_1, cost } Cable; int cmp(void *elem_1, void *elem_2) { int val_1 = (*(Cable *)elem_1).cost; int val_2 = (*(Cable *)elem_2).cost;

Page 15: ... 1576 241833 66 Built into qsort is a function that can swap two given array elements.

...15 2 1

33 3 9

24 1 0

18 5 6

76 2 3

66 1 9

. elem_1 . elem_2

typedef struct { int city_1, city_1, cost } Cable; int cmp(void *elem_1, void *elem_2) { int val_1 = (*(Cable *)elem_1).cost; int val_2 = (*(Cable *)elem_2).cost; if (val_1 < val_2) return -1; if (val_1 > val_2) return 1; return 0; }

Page 16: ... 1576 241833 66 Built into qsort is a function that can swap two given array elements.

...15 2 1

33 3 9

24 1 0

18 5 6

76 2 3

66 1 9

. elem_1 . elem_2

typedef struct { int city_1, city_1, cost } Cable; int cmp(void *elem_1, void *elem_2) { int val_1 = ((Cable *)elem_1)->cost; int val_2 = ((Cable *)elem_2)->cost;

Page 17: ... 1576 241833 66 Built into qsort is a function that can swap two given array elements.

...15 2 1

33 3 9

24 1 0

18 5 6

76 2 3

66 1 9

. elem_1 . elem_2

typedef struct { int city_1, city_1, cost } Cable; int cmp(void *elem_1, void *elem_2) { int val_1 = ((Cable *)elem_1)->cost; int val_2 = ((Cable *)elem_2)->cost; if (val_1 < val_2) return -1; if (val_1 > val_2) return 1; return 0; }

Page 18: ... 1576 241833 66 Built into qsort is a function that can swap two given array elements.

int *A = new int[10]; for (int i=0 ; i < 10 ; i++) A[i] = rand() % 100; qsort(A, 10, sizeof(int), cmp);

...

int cmp (const void *a, const void *b) { if (*(int *)a < *(int *)b) return -1; if (*(int *)a > *(int *)b) return 1; return 0; }

Page 19: ... 1576 241833 66 Built into qsort is a function that can swap two given array elements.

Consider an array of pointers to int.

...

1515 76 24 18 33 66

Page 20: ... 1576 241833 66 Built into qsort is a function that can swap two given array elements.

Consider an array of pointers to int.

...

15

. elem_1 . elem_2

15 76 24 18 33 66

int cmp(void *elem_1, void *elem_2) {

Page 21: ... 1576 241833 66 Built into qsort is a function that can swap two given array elements.

Consider an array of pointers to int.

...

15

. elem_1 . elem_2

15 76 24 18 33 66

int cmp(void *elem_1, void *elem_2) { int val_1 = *(int *)elem_1; int val_2 = *(int *)elem_2;

No Good! elem_1 is a pointer to a pointer!!

Page 22: ... 1576 241833 66 Built into qsort is a function that can swap two given array elements.

Consider an array of pointers to int.

...

15

. elem_1 . elem_2

15 76 24 18 33 66

int cmp(void *elem_1, void *elem_2) { int val_1 = **(int **)elem_1; int val_2 = **(int **)elem_2;

That's it! elem_1 needs to be dereferenced twice!!

Page 23: ... 1576 241833 66 Built into qsort is a function that can swap two given array elements.

Consider an array of pointers to int.

...

15

. elem_1 . elem_2

15 76 24 18 33 66

int cmp(void *elem_1, void *elem_2) { int val_1 = **(int **)elem_1; int val_2 = **(int **)elem_2; if (val_1 < val_2) return -1; if (val_1 > val_2) return 1; return 0; }

Page 24: ... 1576 241833 66 Built into qsort is a function that can swap two given array elements.

typedef struct { int city_1,city_2,cost; } Cable;

...

15

. elem_1 . elem_2

15 2 1

76 3 9

24 1 0

18 5 6

33 2 3

66 1 9

int cmp(void *elem_1, void *elem_2) {

Page 25: ... 1576 241833 66 Built into qsort is a function that can swap two given array elements.

typedef struct { int city_1,city_2,cost; } Cable;

...

15

. elem_1 . elem_2

15 2 1

76 3 9

24 1 0

18 5 6

33 2 3

66 1 9

int cmp(void *elem_1, void *elem_2) { int val_1 = (**(Cable **)elem_1).cost; int val_2 = (**(Cable **)elem_2).cost;

Page 26: ... 1576 241833 66 Built into qsort is a function that can swap two given array elements.

typedef struct { int city_1,city_2,cost; } Cable;

...

15

. elem_1 . elem_2

15 2 1

76 3 9

24 1 0

18 5 6

33 2 3

66 1 9

int cmp(void *elem_1, void *elem_2) { int val_1 = (**(Cable **)elem_1).cost; int val_2 = (**(Cable **)elem_2).cost; if (val_1 < val_2) return -1; if (val_1 > val_2) return 1; return 0; }