You are on page 1of 33

Data Structure

From

Qamaruddin Memon

1
Bubble Sort
• void BubbleSort(int a[], int array_size)
{
int i, j, temp;
for (i = 0; i < (array_size - 1); ++i)
{
for (j = 0; j < array_size - 1 - i; ++j )
{
if (a[j] > a[j+1])
{
temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
}
}
}
}

2
Selection Sort
• void SelectionSort(int a[], int array_size)
{
int i;
for (i = 0; i <N- 1; ++i)
{
int j, min, temp;
min = i;
for (j = i+1; j < N; ++j)
{
if (a[j] < a[min])
min = j;
}

temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}

3
Insertion sort
• void insertionSort(int a[], int array_size)
{
int i, j, index;
for (i = 1; i < N; ++i)
{
index = a[i];
for (j = i; j > 0 && a[j-1] > index; j--)
a[j] = a[j-1];

a[j] = index;
}
}

4
Data Structures and
Abstract Data Type

Problem solving with a computer means processing


data. To process data, we need to define the data type
and the operation to be performed on the data. The
definition of the data type and the definition of the
operation to be applied to the data is part of the idea
behind an abstract data type (ADT)—to hide how the
operation is performed on the data. In other words, the
user of an ADT needs only to know that a set of
operations are available for the data type, but does not
need to know how they are applied.

12.5
ADT example
ADTs
• In this simple example, it does illustrate the concept of an
Abstract Data Type

• ADT consists of
1. The collection of data items
2. Basic operation that must be performed on them

• In the example, consider a train that contain 10 reserved


seats,
• here collection of data is a list of seats
• The basic operations are (1) Scan the list to determine
which seats are occupied (2) change seat’s status
Array ADT
• Collection of data elements
– A fixed-size sequence of elements, all of the same
type

• Basic Operations
– Direct access to each element in the array by
specifying its position so that values can be
retrieved from or stored in that position
Different types of Array
• One-dimensional array: only one index is used
• Multi-dimensional array: array involving more than
one index

• Static array: the compiler determines how memory


will be allocated for the array
One-Dimensional Static Array
• Syntax:
ElementType arrayName [CAPACITY];
ElementType arrayName [CAPACITY] =
{ initializer_list };

• Example:
int b [10];
int b [10]={1,2,3,4,5,6,7,8,9,10};
Array Output Function
Void display(int array[], int num_values)
{
for (int I = 0; i<num_values; i++)
cout<< array[i] << “ ”;
}
Multidimensional Arrays

• Consider a table of test scores for several


different students
Array of Array Declarations
• Each of the rows is itself a one dimensional
array of values

scoresTable
[1][3]
scoresTable[2]

is the whole row


numbered 2
Multidimensional Arrays
• Syntax:
ElementType arrayName [num_rows][num_columns];
STACKS

A stack is a restricted linear list in which all additions


and deletions are made at one end, the top. If we insert a
series of data items into a stack and then remove them,
the order of the data is reversed. This reversing attribute
is why stacks are known as last in, first out (LIFO) data
structures.
Operations on stacks

There are four basic operations, stack, push, pop and empty,
that we define in this chapter.

The stack operation


The stack operation creates an empty stack. The following
shows the format.
The push operation
The push operation inserts an item at the top of the stack.
The following shows the format.
The pop operation
The pop operation deletes the item at the top of the stack.
The following shows the format.
The empty operation
The empty operation checks the status of the stack. The
following shows the format.

This operation returns true if the stack is empty and false if


the stack is not empty.
Stack ADT

We define a stack as an ADT as shown below:


Example

Figure shows a segment of an algorithm that applies the


previously defined operations on a stack S.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAXSIZE 10
void push ();
int pop ();
void display ();
int top=-1;
int stack[MAXSIZE];
void main()
{
int choice;
//clrscr();
do
{
printf("\n1.PUSH");
printf("\n2.POP");
printf("\n3.DISPLAY");
printf("\n4.EXIT");
printf("\nenter your choice");
scanf("\n%d",&choice);
switch(choice)
{
case 1:push();
break;
case 2:
printf("\ndeleted item is
%d",pop());
break;
case 3:
display();
break;
case 4:
printf("\nexit");
exit(0);
break;
}
}
while(choice!=4);
getch();
} Ref:080327HKN EE3110 Power Amplifier (Class A) 24
void push()
{
int item;
if(top==MAXSIZE-1)
{
printf("\nSTACK IS FULL");
getch();
return(0);
}
else
{
printf("\nenter element");
scanf("%d",&item);
top=top+1;
stack[top]=item;
}
}
int pop()
{
int item;
if(top==-1)
{
printf("\nSTACK EMPTY");
getch();
return;
}
else
{
item=stack[top];
top=top-1;
return(item);
}
}

25
void display()
{
int i;
if(top==-1)
{
printf("\nstack is empty");
}
else
{
printf("\nthe elements are:");
for(i=top;i>=0;i--)
{
printf("\t %d",stack[i]);
}
}
}*/

26
QUEUES

A queue is a linear list in which data can only be


inserted at one end, called the rear, and deleted from the
other end, called the front. These restrictions ensure that
the data is processed through the queue in the order in
which it is received. In other words, a queue is a first in,
first out (FIFO) structure.
Operations on queues
Although we can define many operations for a queue, four
are basic: queue, enqueue, dequeue and empty, as defined
below.
The queue operation
The queue operation creates an empty queue. The following
shows the format.
The enqueue operation
The enqueue operation inserts an item at the rear of the
queue. The following shows the format.
The dequeue operation
The dequeue operation deletes the item at the front of the
queue. The following shows the format.
The empty operation
The empty operation checks the status of the queue. The
following shows the format.

This operation returns true if the queue is empty and false if


the queue is not empty.
Example 12.4
Figure 12.12 shows a segment of an algorithm that applies the
previously defined operations on a queue Q.
Queue applications
Queues are one of the most common of all data processing
structures. They are found in virtually every operating
system and network and in countless other areas. For
example, queues are used in online business applications
such as processing customer requests, jobs and orders. In a
computer system, a queue is needed to process jobs and for
system services such as print spools.

You might also like