Professional Documents
Culture Documents
LAB EXERCISES 3
11.10.2016
LAB 3
void main()
{
int a[] = {13,2,34,63,77,34,13,234,234,62,1,11,26,23};
int n=14;
bubbleSort(a,n);
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;
}
_________________________________________________________________________________________
1
LAB 3
#include <iostream>
using namespace std;
void main()
{
int a[] = {13,2,34,63,77,34,13,234,234,62,1,11,26,23};
int n=14;
selectionSort(a,n);
_________________________________________________________________________________________
void main()
{
int a[] = {13,2,34,63,77,34,13,234,234,62,1,11,26,23};
int n=14;
insertionSort(a,n);
2
LAB 3
Tip: For better understanding of this algorithm search videos for Insertion sort expained.
_________________________________________________________________________________________
void main()
{
int a[] = {13,2,34,63,77,34,13,234,234,62,1,11,26,23};
int n=14;
for(;;)
{
while((a[i] <= v) && (i < right))
i++;
3
LAB 3
if(i>=j)
break;
exchange(&a[i], &a[j]);
}
exchange(&a[i], &a[right]);
return i;
}
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>
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);
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;
}
}
5
LAB 3
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;
6
LAB 3
}
}
for(;;)
{
steps++; // access to a[i] (in while)
while((a[i] <= v) && (i < right))
i++;
if(i>=j)
break;
return i;
}
7
LAB 3
delete a; // release it
a = NULL; // and set the pointer to NULL;
}
return a;
}
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];
size = strlen(a);
_________________________________________________________________________________________