Professional Documents
Culture Documents
Array 3
Array 3
Multidimensional Arrays
Syntax (General Format):
type name[size1][size2]...[sizeN];
int multidim[4][10][3];
Disadvantages:
1. not often used
2. large amounts of memory can
be consumed
3. shortage of memory for other
parts of your program
4. quickly out of memory
Sorting an Array
• sorting algorithms:
–quick sort
• divide and conquer
–shaker sort
• it passes alternately
–shell sort
• exchange or sorting by insertion
–bubble sort
• compares adjacent elements and swaps
• The bubble sort gets its name from the way it performs the
sorting operation.
• It uses repeated comparison and, if necessary, exchange of
adjacent elements in the array.
• In this process, small values move toward one end, and
large ones toward the other end.
• The process is conceptually similar to bubbles finding their
own level in a tank of water.
• The bubble sort operates by making several passes through
the array, exchanging out-of-place elements when
necessary.
• The number of passes required to ensure that the array is
sorted is equal to one less than the number of elements in
the array.
Here is the code that forms the core of the bubble sort.
The array being sorted is called nums.
• Notice that the sort relies on two for loops.
1. The inner loop checks adjacent elements in
the array, looking for out-of-order elements.
– When an out-of-order element pair is found, the
two elements are exchanged.
– With each pass, the smallest element of those
remaining moves into its proper location.
2. The outer loop causes this process to repeat
until the entire array has been sorted.
The output is shown here: