Sorting Array Elements. Sams Publishing Object-Oriented Programming in C++
જાહેરાત
જાહેરાત
11 3087 CH10 11/29/01 2:21 PM Page 448
448
Chapter 10
If a group of operators has right associativity, the compiler performs the operation on the right side of the expression first, then works its way to the left. The unary operators such as
* and
++ have right associativity, so the expression is interpreted as
*(ptrd++)
, which increments the pointer, not what it points to. That is, the pointer is incremented first and the dereference operator is applied to the resulting address.
Sorting Array Elements
As a further example of using pointers to access array elements, let’s see how to sort the contents of an array. We’ll use two program examples—the first to lay the groundwork, and the second, an expansion of the first, to demonstrate the sorting process.
Ordering with Pointers
The first program is similar to the
REFORDER program in Chapter 6, “Objects and Classes,” except that it uses pointers instead of references. It orders two numbers passed to it as arguments, exchanging them if the second is smaller than the first. Here’s the listing for
PTRORDER
:
// ptrorder.cpp
// orders two arguments using pointers
#include <iostream> using namespace std; int main()
{ void order(int*, int*); //prototype int n1=99, n2=11; //one pair ordered, one not int n3=22, n4=88; order(&n1, &n2); //order each pair of numbers order(&n3, &n4); cout << “n1=” << n1 << endl; //print out all numbers cout << “n2=” << n2 << endl; cout << “n3=” << n3 << endl; cout << “n4=” << n4 << endl; return 0;
}
//-------------------------------------------------------------void order(int* numb1, int* numb2) //orders two numbers
{ if(*numb1 > *numb2) //if 1st larger than 2nd,
{ int temp = *numb1; //swap them
*numb1 = *numb2;
11 3087 CH10 11/29/01 2:21 PM Page 449
Pointers
449
*numb2 = temp;
}
}
The function order() works the same as it did in
REFORDER
, except that it is passed the addresses of the numbers to be ordered, and it accesses the numbers using pointers. That is,
*numb1 accesses the number in main() passed as the first argument, and
*numb2 accesses the second.
Here’s the output from
PTRORDER
: n1=11
← n2=99
← n3=22
← n4=88
← this and this are swapped, since they weren’t in order this and this are not swapped, since they were in order
We’ll use the order() function from
PTRORDER in our next example program,
PTRSORT
, which sorts an array of integers.
// ptrsort.cpp
// sorts an array using pointers
#include <iostream> using namespace std; int main()
{ void bsort(int*, int); //prototype const int N = 10; //array size
//test array int arr[N] = { 37, 84, 62, 91, 11, 65, 57, 28, 19, 49 }; bsort(arr, N); //sort the array for(int j=0; j<N; j++) //print out sorted array cout << arr[j] << “ “; cout << endl; return 0;
}
//-------------------------------------------------------------void bsort(int* ptr, int n)
{ void order(int*, int*); //prototype int j, k; //indexes to array for(j=0; j<n-1; j++) //outer loop for(k=j+1; k<n; k++) //inner loop starts at outer order(ptr+j, ptr+k); //order the pointer contents
}
10
11 3087 CH10 11/29/01 2:21 PM Page 450
450
Chapter 10
//-------------------------------------------------------------void order(int* numb1, int* numb2) //orders two numbers
{ if(*numb1 > *numb2) //if 1st larger than 2nd,
{ int temp = *numb1; //swap them
*numb1 = *numb2;
*numb2 = temp;
}
}
The array arr of integers in main() is initialized to unsorted values. The address of the array, and the number of elements, are passed to the bsort() function. This sorts the array, and the sorted values are then printed. Here’s the output of the
PTRSORT
:
11 19 28 37 49 57 62 65 84 91
The Bubble Sort
The bsort() function sorts the array using a variation of the bubble sort. This is a simple
(although notoriously slow) approach to sorting. Here’s how it works, assuming we want to arrange the numbers in the array in ascending order. First the first element of the array
( arr[0]
) is compared in turn with each of the other elements (starting with the second). If it’s greater than any of them, the two are swapped. When this is done we know that at least the first element is in order; it’s now the smallest element. Next the second element is compared in turn with all the other elements, starting with the third, and again swapped if it’s bigger. When we’re done we know that the second element has the second-smallest value. This process is continued for all the elements until the next-to-the-last, at which time the array is assumed to be ordered. Figure 10.8 shows the bubble sort in action (with fewer items than in
PTRSORT
).
In
PTRSORT
, the number in the first position, 37, is compared with each element in turn, and swapped with 11. The number in the second position, which starts off as 84, is compared with each element. It’s swapped with 62; then 62 (which is now in the second position) is swapped with 37, 37 is swapped with 28, and 28 is swapped with 19. The number in the third position, which is 84 again, is swapped with 62, 62 is swapped with 57, 57 with 37, and 37 with 28. The process continues until the array is sorted.
The bsort() function in
PTRSORT consists of two nested loops, each of which controls a pointer. The outer loop uses the loop variable j
, and the inner one uses k
. The expressions ptr+j and ptr+k point to various elements of the array, as determined by the loop variables.
The expression ptr+j moves down the array, starting at the first element (the top) and stepping down integer by integer until one short of the last element (the bottom). For each position taken by ptr+j in the outer loop, the expression ptr+k in the inner loop starts pointing one below ptr+j and moves down to the bottom of the array. Each time through the inner loop, the
11 3087 CH10 11/29/01 2:21 PM Page 451
Pointers
451 elements pointed to by ptr+j and ptr+k are compared, using the order() function, and if the first is greater than the second, they’re swapped. Figure 10.9 shows this process.
F
IGURE
10.8
Operation of the bubble sort.
The
PTRSORT example begins to reveal the power of pointers. They provide a consistent and efficient way to operate on array elements and other variables whose names aren’t known to a particular function.
10
ડાઉનલોડ કરો
જાહેરાત