Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Multi – Dimensional Arrays

Multidimensional Arrays
Syntax (General Format):

type name[size1][size2]...[sizeN];

For example, the following declaration


creates a 4×10×3–integer array:

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:

• Original array is: 41 18467 6334 26500 19169 15724


11478 29358 26962 24464

• Sorted array is: 41 6334 11478 15724 18467 19169


24464 26500 26962 29358

You might also like