0% found this document useful (0 votes)
95 views5 pages

Exercise 1: Source Code:: Atif Jalal 02-235191-027 BS (IT) - 3A Lab 10 Date: 14 July, 2020

The document contains source code for 4 exercises involving sorting and recursion algorithms. Exercise 1 contains code to calculate the factorial of a number. Exercise 2 contains code to print the Fibonacci sequence. Exercise 3 contains code to implement merge sort to sort an array. Exercise 4 contains code to implement quicksort to sort an array. For each exercise, the code is provided along with sample input/output.

Uploaded by

Atif Jalal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views5 pages

Exercise 1: Source Code:: Atif Jalal 02-235191-027 BS (IT) - 3A Lab 10 Date: 14 July, 2020

The document contains source code for 4 exercises involving sorting and recursion algorithms. Exercise 1 contains code to calculate the factorial of a number. Exercise 2 contains code to print the Fibonacci sequence. Exercise 3 contains code to implement merge sort to sort an array. Exercise 4 contains code to implement quicksort to sort an array. For each exercise, the code is provided along with sample input/output.

Uploaded by

Atif Jalal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Atif Jalal

02-235191-027
BS (IT)-3A Lab 10 Date: 14 July, 2020

Exercise 1:
Source Code:
#include <iostream>
using namespace std;

int factorial (int);

void main()
{
int num;
line:
cout << "\n Enter a positive integer : ";
cin >> num;
if (num < 0)
{
cout << "\n Negitive numbers hae no factorial." << endl;
goto line;
}
else
{
cout << " " <<num <<" Factorial = " << factorial(num) << endl;
}
system("PAUSE");
}

int factorial(int n)
{
if (n <= 1)
return 1;
else
return (n * factorial(n - 1));
}
Atif Jalal
02-235191-027
BS (IT)-3A Lab 10 Date: 14 July, 2020
Output:

Exercise 2:
Source Code:
#include <iostream>
using namespace std;

int fab(int);

int main()
{
int N;
cout << "\n Enter a positive number : ";
cin >> N;
if (N <= 0)
cout << " Enter a correct number " << endl;
else
{
cout << "\n";
cout << " Fibonacci series is : ";
for (int i = 1; i <= N; i++)
{
cout << fab(i - 1) << ",";
}
cout << "\n";
}
system("pause");
return 0;
}
int fab(int x)
{
if (x == 0)
return 0;
else if (x == 1)
return 1;
else
return (fab(x - 1) + fab(x - 2));
}

Output:
Atif Jalal
02-235191-027
BS (IT)-3A Lab 10 Date: 14 July, 2020
Exercise 3:
Source Code:
#include <iostream>
using namespace std;

void merge_sort(int *arr, int low, int high);


void merge(int *arr, int low, int high, int mid);

int main()
{
int myarray[30], num;
cout << "\n Enter number of elements to be sorted : ";
cin >> num;
cout << "\n Enter " << num << " elements to be sorted : ";
for (int i = 0; i < num; i++)
{
cin >> myarray[i];
}
merge_sort(myarray, 0, num - 1);
cout << "\n Sorted array : {";
for (int i = 0; i < num; i++)
{
cout << myarray[i] << ",";
}
cout << "\b}" <<endl;
system("pause");
}
void merge_sort(int *arr, int low, int high)
{
int mid;
if (low < high)
{
mid = (low + high) / 2;
merge_sort(arr, low, mid);
merge_sort(arr, mid + 1, high);
merge(arr, low, high, mid);
}
}

void merge(int *arr, int low, int high, int mid)


{
int i, j, k, c[50];
i = low;
k = low;
j = mid + 1;
while (i <= mid && j <= high) {
if (arr[i] < arr[j]) {
c[k] = arr[i];
k++;
i++;
}
else {
c[k] = arr[j];
k++;
j++;
}
}
while (i <= mid) {
c[k] = arr[i];
Atif Jalal
02-235191-027
BS (IT)-3A Lab 10 Date: 14 July, 2020
k++;
i++;
}
while (j <= high) {
c[k] = arr[j];
k++;
j++;
}
for (i = low; i < k; i++) {
arr[i] = c[i];
}
}

Output:

Exercise 4:
Source Code:
#include <iostream>
using namespace std;

void QuickSort(int[], int, int);


int partition(int[], int, int);

int main()
{
int arr[50], n;
cout << "\n Enter number of elements : ";
cin >> n;
cout << "\n Enter the array elements : ";
for (int i = 0; i<n; i++)
{
cin >> arr[i];
}
QuickSort(arr, 0, n - 1);
cout << "\n sorted array : {";
for (int a = 0; a < n; a++)
{
cout << arr[a] << ",";
}
cout << "\b}" << endl;
system("PAUSE");
return 0;
}

void QuickSort(int arr[], int start, int end)


{
int pindex;
if (start < end)
Atif Jalal
02-235191-027
BS (IT)-3A Lab 10 Date: 14 July, 2020
{
pindex = partition(arr, start, end);
QuickSort(arr, start, pindex - 1);
QuickSort(arr, pindex + 1, end);
}
}

int partition(int arr[], int start, int end)


{
int pivot = arr[end];
int i = start;
for (int j = i; j <= end - 1; j++)
{
if (arr[j] <= pivot)
{
swap(arr[j], arr[i]);
i++;
}
}
swap(arr[i], arr[end]);
return i;
}

Output:

You might also like