You are on page 1of 10

DATA STRUCTURES AND ALGORITHMS

LAB EXERCISES 3

ALGORITHMS, O-NOTATION AND SORTING

11.10.2016
LAB 3

Example 1: Sort an array with "Bubble sort" algorithm


#include <iostream>
using namespace std;

void exchange(int *, int *);


void bubbleSort(int [], int);

void main()
{
int a[] = {13,2,34,63,77,34,13,234,234,62,1,11,26,23};
int n=14;

bubbleSort(a,n);

for(int i=0; i<n; i++){


cout<<a[i]<<" ";
}
}

void exchange(int *a, int *b)


{
int temp = *a;
*a = *b;
*b = temp;
}

void bubbleSort(int a[], int size)


{
for(int i = 0; i < size; i++)
for(int j = size-1; j > i; j--)
if(a[i] > a[j])
exchange(&a[i], &a[j]);
}

Note: Note that the exchange function can be call also this way:
void exchange(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}

void bubbleSort(int a[], int size)


{
for(int i = 0; i < size; i++)
for(int j = size-1; j > i; j--)
if(a[i] > a[j])
exchange(a[i], a[j]);
}

_________________________________________________________________________________________

Example 2: Sort an array with "Selection sort" algorithm

1
LAB 3

#include <iostream>
using namespace std;

void exchange(int &, int &);


void selectionSort(int [], int);

void main()
{
int a[] = {13,2,34,63,77,34,13,234,234,62,1,11,26,23};
int n=14;

selectionSort(a,n);

for(int i=0; i<n; i++){


cout<<a[i]<<" ";
}
}

void exchange(int &a, int &b)


{
int temp = a;
a = b;
b = temp;
}

void selectionSort(int a[], int size)


{
for(int i = 0; i < size-1; i++){
int indexmin = i;
for(int j = i+1; j < size; j++){
if(a[j] < a[indexmin]){
indexmin = j;
}
}
exchange(a[i], a[indexmin]);
}
}

_________________________________________________________________________________________

Example 3: Sort an array with the "Insertion sort" algorithm


#include <iostream>
using namespace std;

void insertionSort(int [], int);

void main()
{
int a[] = {13,2,34,63,77,34,13,234,234,62,1,11,26,23};
int n=14;

insertionSort(a,n);

for(int i=0; i<n; i++){


cout<<a[i]<<" ";
}
}

2
LAB 3

void insertionSort(int a[], int size)


{
for(int i = 1; i < size; i++)
{
int flow = a[i];
int j = i-1;
while ((j>=0) && (a[j] > flow))
{
a[j+1] = a[j];
j=j-1;
}
a[j+1] = flow;
}
}

Tip: For better understanding of this algorithm search videos for Insertion sort expained.
_________________________________________________________________________________________

Example 4: Sort an array with the "Quick sort" algorithm


#include <iostream>
using namespace std;

void exchange(int *, int *);


int split(int [], int, int);
void quickSort(int [], int, int);

void main()
{
int a[] = {13,2,34,63,77,34,13,234,234,62,1,11,26,23};
int n=14;

quickSort(a,0,n-1); //0 is th left index, n-1 is the right index

for(int i=0; i<n; i++){


cout<<a[i]<<" ";
}
}

void exchange(int *a, int *b)


{
int temp = *a;
*a = *b;
*b = temp;
}

int split(int a[], int left, int right)


{
int i = left, j = right-1;
int v = a[right];

for(;;)
{
while((a[i] <= v) && (i < right))
i++;

3
LAB 3

while((a[j] >= v) && (j > left))


j--;

if(i>=j)
break;

exchange(&a[i], &a[j]);
}

exchange(&a[i], &a[right]);

return i;
}

void quickSort(int a[], int left, int right)


{
if(right-left < 1)
return;

int spot = split(a, left, right);

quickSort(a, left, spot-1);


quickSort(a, spot+1, right);
}

Tip: For better understanding of this algorithm search videos for Quick sort explained.
_________________________________________________________________________________________

Exercise 1: Make a program that creates a random array of integers. The user chooses which
algorithm will be used for sorting that array by entering a number from the keyboard. At the
end print the sorted array and print the number of steps during the execution of each of the
algorithms.
#include <iostream>
using namespace std;
#include <ctime>

int * randomArray(bool, int [], int *);


void createArray(int [], const int);
void printArray(const int [], const int,int&);
void printArray(const int [], const int);

void exchange(int *, int *, int&);


void selectionSort(int [], int, int, int&);
void insertionSort(int [], int, int, int&);
void bubbleSort(int [], int, int, int&);
int split(int [], int, int, int&);
void quicksort(int [], int, int, int&);

void main()
{
int *a = NULL; // the name of the array (pointer)
int size; // the size of the array
int *sizePointer = &size; // pointer to the size of the array

char choice;

4
LAB 3

int steps = 0;

cout << "Enter 0 for pseudo-random array or any other character for random-like
array: ";
cin >> choice;
cout << endl;

if(choice == '0')
a = randomArray(false, a, sizePointer);
else
a = randomArray(true, a, sizePointer);

cout << "\nChoose a sorting algorithm: " << endl;


cout << "1 - Selection sort" << endl;
cout << "2 - Insertion sort" << endl;
cout << "3 - Bubble sort" << endl;
cout << "4 - Quicksort" << endl;
cout << "Any other character - EXIT" << endl;
cout << "\nYour choice is: ";
cin >> choice;

switch(choice)
{
case '1': selectionSort(a, 0, size-1, steps);
printArray(a, size, steps);
break;
case '2': insertionSort(a, 0, size-1, steps);
printArray(a, size, steps);
break;
case '3': bubbleSort(a, 0, size-1, steps);
printArray(a, size, steps);
break;
case '4': quicksort(a, 0, size-1, steps);
printArray(a, size, steps);
break;
default: return;

}
}

void exchange(int *a, int *b, int &steps)


{
steps++; // access to an argument (*a)
int temp = *a;

steps+=2; // access to arguments (*a i *b)


*a = *b;

steps++; // access to an argument (*b)


*b = temp;
}

void selectionSort(int a[], int left, int right, int &steps)


{
for(int i = left; i < right; i, i++)
{
int indexmin = i;

5
LAB 3

for(int j = i+1; j <= right; j++)


{
steps+=2; // access to a[j] and a[indexmin] (in if)
if(a[j] < a[indexmin])
{
indexmin = j;
}
}

exchange(&a[i], &a[indexmin], steps);


}
}

void insertionSort(int a[], int left, int right, int& steps)


{
for(int i = left+1; i <= right; i++)
{
steps++; // access to a[i]
int flow = a[i];

int j = i-1;

while (true)
if(j>=0)
{
steps++; // access to a[j] (in if)
if(a[j] > flow)
{
steps+=2; // access to a[j+1] and a[j]
a[j+1] = a[j];

j=j-1;
}
else
break;
}
else
break;

steps++; // access to a[j+1]


a[j+1] = flow;
}
}

void bubbleSort(int a[], int left, int right, int &steps)


{
for(int i = 0; i < right; i++)
{
for(int j = right; j > i; j--)
{
steps+=2; // access to a[i] and a[j] (in if)
if(a[i] > a[j])
{
exchange(&a[i], &a[j], steps);
}
}

6
LAB 3

}
}

int split(int a[], int left, int right, int& steps)


{
int i = left, j = right-1;

steps++; // access to a[right]


int v = a[right];

for(;;)
{
steps++; // access to a[i] (in while)
while((a[i] <= v) && (i < right))
i++;

steps++; // access to a[i] (in while)


while((a[j] >= v) && (j > left))
j--;

if(i>=j)
break;

exchange(&a[i], &a[j], steps);


}

exchange(&a[i], &a[right], steps);

return i;
}

void quicksort(int a[], int left, int right, int &steps)


{
if(right-left < 1)
return;

int spot = split(a, left, right, steps);

quicksort(a, left, spot-1, steps);


quicksort(a, spot+1, right, steps);
}

int * randomArray(bool randomLike, int *a, int *sizePointer)


{
*sizePointer = 0;

if(randomLike) // if the array has to be with random-like numbers,


srand(time(0));// initialize srand to the value returned by function time
else // else
srand(1); // generate the same array

while (*sizePointer == 0) // as long as the size is zero,


*sizePointer = rand() % 101; // generate radom numbers between 0 and 100
(101 - 1)
//(since *sizePointer is an integer)

if (a != NULL) // if the array was already allocated a memory,


{

7
LAB 3

delete a; // release it
a = NULL; // and set the pointer to NULL;
}

a = new int[*sizePointer]; // allocate new memory for the new array

createArray(a, *sizePointer); // create the array


printArray(a, *sizePointer); //and print it on screen

return a;
}

void createArray(int a[], const int size)


{
int i; // local counter

for(i = 0; i < size; i++)


a[i] = rand() % RAND_MAX;//random integer number between 0 and RAND_MAX - 1
//(since a[i] is an integer)
//RAND_MAX is defined and usually has the value of 32767
}

void printArray(const int a[], const int size, int& steps)


{
int i; // local counter

cout << "size = " << size << endl;

for(i = 0; i < size; i++)


cout <<"a[" << i << "]=" << a[i] << ((a[i] < 100) ? "\t " : "\t");

cout <<endl<<"The algorithm has finished in "<<steps<<" steps."<<endl;


}

void printArray(const int a[], const int size)


{
int i; // local counter

cout << "size = " << size << endl;

for(i = 0; i < size; i++)


cout <<"a[" << i << "]=" << a[i] << ((a[i] < 100) ? "\t " : "\t");
}
_________________________________________________________________________________________

Exercise 2: Change the main function of exercise 1 so you can read the array from standard
input.
cout << "Number of elements in the array? ";
cin >> size;
a = new int[size];
for(int i = 0; i < size; i++)
{
cout << "a[" << i << "] = ";
cin >> a[i];
}

8
LAB 3

_________________________________________________________________________________________

Exercise 3: Change the main function of exercise 1 so you can read a character array (String)
from standard input
char *a = new char[50];

cout << "Enter the string to be sorted: ";


cin.getline(a, 50);

size = strlen(a);

_________________________________________________________________________________________

You might also like