Professional Documents
Culture Documents
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
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
• 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
• 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
scoresTable
[1][3]
scoresTable[2]
There are four basic operations, stack, push, pop and empty,
that we define in this chapter.
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