DATA STRUCTURES

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

CHAPTER 6

DATA STRUCTURES
These refer to groups of data elements that are organized in a single unit so that they can be
used more efficiently as compared to the simple data types such as integers and strings. An
example of a data structure is the array. Ordinary variables store one value at a time while an
array will store more than one value at a time in a single variable name.
Data structures are important for grouping sets of similar data together and passing them as one.

Classification of Data Structures


a) Linear
In linear data structures, values are arranged in linear fashion. A linear data structure traverses
the data elements sequentially. The elements in the structure are adjacent to one another other
and every element has exactly two neighbor elements to which it is connected. Arrays, linked
lists, stacks and queues are examples of linear data structures.
b) Non-Linear
The data values in this structure are not arranged in order but every data item is attached to
several other data items in a way that is specific for reflecting relationships. Tree, graph, table
and sets are examples of non-linear data structures.
c) Homogenous
In this type of data structures, values of the same types of data are stored, as in an array.
d) Non-homogenous
In this type of data structures, data values of different types are grouped, as in structures and
classes.
e) Dynamic
In dynamic data structures such as references and pointers, size and memory locations can be
changed during program execution. These data structures can grow and shrink during execution.
f) Static
With a static data structure, the size of the structure is fixed. Static data structures such as arrays
are very good for storing a well-defined number of data items.

1
Arrays
An array is a named list of elements, all with the same data type. It is better defined as a
consecutive group of memory locations all of which have the same name and the same data type.
Arrays store a fixed-size sequential collection of elements of the same type.
All arrays consist of contiguous memory locations. The lowest address corresponds to the first
element and the highest address to the last element.

Declaring Arrays
To declare an array in C, a programmer specifies the type of the elements and the number of
elements required by an array as follows:
type arrayName [ arraySize ];
This is called a single-dimensional array. The arraySize must be an integer constant greater than
zero and type can be any valid C data type.
For example, to declare a 10-element array called balance of type double, use this statement:
double balance[10];

Initializing Arrays
You can initialize an array in C either one by one or using a single statement as follows:
double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
The number of values between braces { } cannot be larger than the number of elements that we
declare for the array between square brackets [ ]. Following is an example to assign a single
element of the array:
If you omit the size of the array, an array just big enough to hold the initialization is created.
Therefore, if you write:
double balance[] = {1000.0, 2.0, 3.4, 17.0, 50.0};
You will create exactly the same array as you did in the previous example.
balance[4] = 50.0;
The above statement assigns element number 5th in the array a value of 50.0. Array with 4th
index will be 5th ie. last element because all arrays have 0 as the index of their first element
which is also called base index. Following is the pictorial representation of the same array we
discussed above:

2
Accessing Array Elements
An element is accessed by indexing the array name. This is done by placing the index
of the element within square brackets after the name of the array. For example:
double salary = balance[9];
The above statement will take 10th element from the array and assign the value to salary
variable. Following is an example which will use all the above mentioned three concepts i.e.
declaration, assignment and accessing arrays:

#include <stdio.h>
int main ()
{
int n[ 10 ]; /* n is an array of 10 integers */
int i,j;
/* initialize elements of array n to 0 */
for ( i = 0; i < 10; i++ )
{
n[ i ] = i + 100; /* set element at location i to i
+ 100 */
}
/* output each array element's value */
for (j = 0; j < 10; j++ )
{
printf("Element[%d] = %d\n", j, n[j] );
}
return 0;
}

When the above code is compiled and executed, it produces the following result:
Element[0] = 100
Element[1] = 101
Element[2] = 102
Element[3] = 103
Element[4] = 104
Element[5] = 105
Element[6] = 106
Element[7] = 107
Element[8] = 108
Element[9] = 109

3
Sort Techniques
Bubble Sort
In the bubble sort, as elements are sorted they gradually "bubble" (or rise) to their proper
location in the array, like bubbles rising in a glass of soda. The bubble sort repeatedly compares
adjacent elements of an array. The first and second elements are compared and swapped if out of
order. Then the second and third elements are compared and swapped if out of order. This
sorting process continues until the last two elements of the array are compared and swapped if
out of order.

When this first pass through the array is complete, the bubble sort returns to elements one and
two and starts the process all over again.

The table below follows an array of numbers before, during, and after a bubble sort
fordescending order. A "pass" is defined as one full trip through the array comparing and if
necessary, swapping, adjacent elements. Several passes have to be made through the array
before it is finally sorted

The bubble sort is an easy algorithm to program, but it is slower than many other sorts. With a
bubble sort, it is always necessary to make one final "pass" through the array to check to see that
no swaps are made to ensure that the process is finished. In actuality, the process is finished
before this last pass is made.

Exchange Sort
The exchange sort is similar to its cousin, the bubble sort, in that it compares elements of the
array and swaps those that are not in their proper positions. (Some people refer to the "exchange
sort" as a "bubble sort".) The difference between these two sorts is the manner in which they
compare the elements. The exchange sort compares the first element with each following
element of the array, making any necessary swaps.

4
When the first pass through the array is complete, the exchange sort then takes the second
element and compares it with each following element of the array swapping elements that are out
of order. This sorting process continues until the entire array is ordered.
Let's examine our same table of elements again using an exchange sort for descending order.
Remember, a "pass" is defined as one full trip through the array comparing and if necessary,
swapping elements.

The exchange sort, in some situations, is slightly more efficient than the bubble sort.
It is not necessary for the exchange sort to make that final complete pass needed by the bubble
sort to determine that it is finished.

Selection Sort
The selection sort is a combination of searching and sorting.
During each pass, the unsorted element with the smallest (or largest) value is moved to its
proper position in the array.
The number of times the sort passes through the array is one less than the number of items in the
array. In the selection sort, the inner loop finds the next smallest (or largest) value and the outer
loop places that value into its proper location.
Let's look at our same table of elements using a selection sort for descending order.
Remember, a "pass" is defined as one full trip through the array comparing and if necessary,
swapping elements.

5
While being an easy sort to program, the selection sort is one of the least efficient.
The algorithm offers no way to end the sort early, even if it begins with an already sorted list.

Shell Sort
Instead of comparing adjacent elements, like the bubble sort, the shell sort repeatedly compares
elements that are a certain distance away from each other (d represents this distance). The value
of d starts out as half the input size and is halved after each pass through the array.
The elements are compared and swapped when needed. The equation d= (N + 1) / 2 is used.
Notice that only integer values are used for d since integer division is occurring.
Let's look at our same list of values for descending order with the shell sort.
Remember, a "pass" is defined as one full trip through the array comparing and if necessary,
swapping elements.

First Pass: d = (6 + 1) / 2 = 3. Compare 1st and 4th , 2nd and 5th, and 3rd and 6th items since
they are 3 positions away from each other))
Second Pass: value for d is halved d = (3 + 1) / 2 = 2. Compare items two places away such as
1st and 3rd ……
Third Pass: value for d is halved d = (2 + 1) / 2 = 1. Compare items one place away such as 1st
and 2nd …..
Last Pass: sort continues until d = 1 and the pass occurs without any swaps.
This sorting process, with its comparison model, is an efficient sorting algorithm.

6
Quick Sort
The quicksort is considered to be very efficient, with its "divide and conquer" algorithm. This
sort starts by dividing the original array into two sections (partitions) based upon the value of the
first element in the array. Since our example sorts into descending order, the first section will
contain all the elements greater than the first element. The second section will contain elements
less than (or equal to) the first element. It is possible for the first element to end up in either
section.

Merge Sort
The merge sort combines two sorted arrays into one larger sorted array.
As the diagram below shows, Array A and Array B merge to form Array C.
Arrays to be merged MUST be SORTED FIRST!!
Be sure to declare Array C in main ( ) and establish its size.
Example: Ascending Order
Array A: {7. 12}
Array B: {5, 7, 8}
Array C: {5, 7, 7, 8, 12} after merge Here is how it works: The first element of array A is
compared with the first element of array B. If the first element of array A is smaller than the first
element of array B, the element from array A is moved to the new array C. The subscript of array
A is now increased since the first element is now set and we move on.
If the element from array B should be smaller, it is moved to the new array C. The subscript of
array B is increased. This process of comparing the elements in the two arrays continues until
either array A or array B is empty. When one array is empty, any elements remaining in the other
(non-empty) array are "pushed" into the end of array C and the merge is complete.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy