0% found this document useful (0 votes)
44 views

Software Engineering With C++

The document describes algorithms and C++ programs to implement various sorting algorithms using objects as parameters, including bubble sort, insertion sort, merge sort, quick sort, and heap sort. For each algorithm, pseudo code is provided to describe the sorting approach and C++ code is provided to implement the algorithm by taking user input, calling relevant functions to perform the sorting, and outputting the results.

Uploaded by

Rahul Roy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Software Engineering With C++

The document describes algorithms and C++ programs to implement various sorting algorithms using objects as parameters, including bubble sort, insertion sort, merge sort, quick sort, and heap sort. For each algorithm, pseudo code is provided to describe the sorting approach and C++ code is provided to implement the algorithm by taking user input, calling relevant functions to perform the sorting, and outputting the results.

Uploaded by

Rahul Roy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

ASSIGNMENT NO:-4

1.Write a C++ program to implement Bubble Sort using object as parameter.

ALGORITHM
1. Take input of data.
2. Call BubbleSort() function with ‘arr’ the array of data and ‘n’ the number of values, in the
argument list.
3. Implement Sorting algorithm using nested for loop.
4. The first loop will run on ‘i’ from 0 to n-1.
5. The second loop will run on ‘j’ from 0 to n-i-1.
6. Compare two consecutive values.
7. Switch the values if arr[j+1] <arr[j].
8. Return to main and display the result.
9. Exit.

CODE-
#include <iostream>
using namespace std;
void BubbleSort (int arr[], int n)
{
int i, j;
for (i = 0; i < n; ++i)
{
for (j = 0; j < n-i-1; ++j)
{
if (arr[j] > arr[j+1])
{
arr[j] = arr[j]+arr[j+1];
arr[j+1] = arr[j]-arr[j + 1];
arr[j] = arr[j]-arr[j + 1];
}
}
}
}
int main()
{
int n, i;
cout<<"---- Name : Rahul Roy, Section : CSE-3D, Roll No : 43 ----"<<endl; cout<<"\nEnter the
number of data element to be sorted: ";
cin>>n;
int arr[n];
for(i = 0; i < n; i++)
{
cout<<"Enter element "<<i+1<<": ";
cin>>arr[i];
}
BubbleSort(arr, n);
cout<<"\nSorted Data ";
for (i = 0; i < n; i++)
cout<<"->"<<arr[i];
return 0;
}

OUTPUT-

2.Write a C++ program to implement Insertion Sort using object as parameter

ALGORITHM
1. Create a head node of the list structure.
2. Take input of data and simultaneously insert it into a list using InsertinList().
3. Assign the new element as newnode.
4. If the head is null then assign newnode to head.
5. Otherwise, insert the newnode so that list remains sorted.
6. Return head to main.
7. Display the result.
8. Exit.

CODE-
#include <iostream>
using namespace std;
// A structure to represent a node.
struct list
{
int data;
list *next;
};
// Function implementing insertion sort.
list* InsertinList(list *head, int n)
{
// Creating newnode and temp node.
list *newnode = new list;
list *temp = new list;
// Using newnode as the node to be inserted in the list.
newnode->data = n;
newnode->next = NULL;
// If head is null then assign new node to head.
if(head == NULL)
{
head = newnode;
return head;
}
else
{
temp = head;
// If newnode->data is lesser than head->data, then insert
newnode before head.
if(newnode->data < head->data)
{
newnode->next = head;
head = newnode;
return head;
}
// Traverse the list till we get value more than newnode-
>data.
while(temp->next != NULL)
{
if(newnode->data < (temp->next)->data)
break;

temp=temp->next;
}
// Insert newnode after temp.
newnode->next = temp->next;
temp->next = newnode;
return head;
}
}
int main()
{
int n, i, num;
// Declaring head of the linked list.
list *head = new list;
head = NULL;
cout<<"---- Name : Rahul Roy, Section : CSE-3D, Roll No : 43 ----
"<<endl;
cout<<"\nEnter the number of data element to be sorted: ";
cin>>n;
for(i = 0; i < n; i++)
{
cout<<"Enter element "<<i+1<<": ";
cin>>num;
// Inserting num in the list.
head = InsertinList(head, num);
}
// Display the sorted data.
cout<<"\nSorted Data ";
while(head != NULL)
{
cout<<"->"<<head->data;
head = head->next;
}
return 0;
}

OUTPUT-
3.Write a C++ program to implement Merge Sort using object as parameter.

ALGORITHM
1. Take input of data.
2. Call MergeSort() function.
3. Recursively split the array into two equal parts.
4. Split them until we get at most one element in both half.
5. Combine the result by invoking Merge().
6. It combines the individually sorted data from low to mid and mid+1 to high.
7. Return to main and display the result.
8. Exit.

CODE-
#include <iostream>
using namespace std;
void Merge(int *a, int low, int high, int mid)
{
int i, j, k, temp[high-low+1];
i = low;
k = 0;
j = mid + 1;
while (i <= mid && j <= high)
{
if (a[i] < a[j])
{
temp[k] = a[i];
k++;
i++;
}
else
{
temp[k] = a[j];
k++;
j++;
}
}
while (i <= mid)
{
temp[k] = a[i];
k++;
i++;
}
while (j <= high)
{
temp[k] = a[j];
k++;
j++;
for (i = low; i <= high; i++)
{
a[i] = temp[i-low];
}
}
void MergeSort(int *a, int low, int high)
{
int mid;
if (low < high)
{
mid=(low+high)/2;
MergeSort(a, low, mid);
MergeSort(a, mid+1, high);
Merge(a, low, high, mid);
}
}
int main()
{
int n, i;
cout<<"---- Name : Rahul Roy, Section : CSE-3D, Roll No : 43 ----"<<endl;
cout<<"\nEnter the number of data element to be sorted: ";
cin>>n;

int arr[n];
for(i = 0; i < n; i++)
{
cout<<"Enter element "<<i+1<<": ";
cin>>arr[i];
}
MergeSort(arr, 0, n-1);
// Printing the sorted data.
cout<<"\nSorted Data ";
for (i = 0; i < n; i++)
cout<<"->"<<arr[i];

return 0;
}

OUTPUT-
4. Write a C++ program to implement Quick Sort using object as parameter.

ALGORITHM
1. Take input of data.
2. Call QuickSort() function.
3. Through RandomPivotPartition(), select pivot randomly.
4. Create a partition of the array on the basis of the pivot so that the element to the left of the pivot
are smaller and to the right are greater.
5. Recursively insert the partitions into QuickSort() and repeat step 2 until low is lesser than high.
6. Return to main and display the result.
7. Exit.

CODE-
#include<iostream>
#include<cstdlib>
using namespace std;
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int Partition(int a[], int low, int high)
{
int pivot, index, i;
index = low;
pivot = high;
for(i=low; i < high; i++)
{
if(a[i] < a[pivot])
{
swap(&a[i], &a[index]);
index++;
}
}
swap(&a[pivot], &a[index]);

return index;
}
int RandomPivotPartition(int a[], int low, int high)
{
int pvt, n, temp;
n = rand();
pvt = low + n%(high-low+1);
swap(&a[high], &a[pvt]);

return Partition(a, low, high);


}
int QuickSort(int a[], int low, int high)
{
int pindex;
if(low < high)
{
pindex = RandomPivotPartition(a, low, high);
QuickSort(a, low, pindex-1);
QuickSort(a, pindex+1, high);
}
return 0;
}

int main()
{
int n, i;
cout<<"---- Name : Rahul Roy, Section : CSE-3D, Roll No : 43 ----"<<endl;
cout<<"\nEnter the number of data element to be sorted: ";
cin>>n;

int arr[n];
for(i = 0; i < n; i++)
{
cout<<"Enter element "<<i+1<<": ";
cin>>arr[i];
}
QuickSort(arr, 0, n-1);
cout<<"\nSorted Data ";
for (i = 0; i < n; i++)
cout<<"->"<<arr[i];

return 0;
}

OUTPUT-
5. Write a C++ program to implement Heap Sort using object as parameter.

ALGORITHM
1. Take input of data.
2. Call Build_MaxHeap() function with ‘arr’ the array of data and ‘n-1’ the number of values, in the
argument list.
3. After building the max heap call HeapSort().
4. Switch the root value of heap with the last index value of array since root value is highest among
all.
5. Decrement the last index value.
6. Repeat it for all the element.
7. Return to main and display the result.
8. Exit.

CODE-
#include <iostream>
using namespace std;
void MaxHeapify(int a[], int i, int n)
{
int j, temp;
temp = a[i];
j = 2*i;
while (j <= n)
{
if (j < n && a[j+1] > a[j])
j = j+1;
if (temp > a[j])
break;
else if (temp <= a[j])
{
a[j/2] = a[j];
j = 2*j;
}
}
a[j/2] = temp;
return;
}
void HeapSort(int a[], int n)
{
int i, temp;
for (i = n; i >= 2; i--)
{
temp = a[i];
a[i] = a[1];
a[1] = temp;
MaxHeapify(a, 1, i - 1);
}
}
void Build_MaxHeap(int a[], int n)
{
int i;
for(i = n/2; i >= 1; i--)
MaxHeapify(a, i, n);
}
int main()
{
int n, i;
cout<<"---- Name : Rahul Roy, Section : CSE-3D, Roll No : 43 ----"<<endl;
cout<<"\nEnter the number of data element to be sorted: ";
cin>>n;
n++;
int arr[n];
for(i = 1; i < n; i++)
{
cout<<"Enter element "<<i<<": ";
cin>>arr[i];
}
Build_MaxHeap(arr, n-1);
HeapSort(arr, n-1);
cout<<"\nSorted Data ";
for (i = 1; i < n; i++)
cout<<"->"<<arr[i];
return 0;
}

OUTPUT-
6.Write a C++ program to implement Binary search using object as parameter.

ALGORITHM
1. Assign the data to the array in a sorted manner.
2. Call BinarySearch() function with ‘arr’ the array of data and ‘n’ the number of values, start and end
index, iteration count and s[0] be the element to be searched in the argument list.
3. Increment the iteration counter and compare the item value with the a[mid].
4. If item < a[mid] choose first half otherwise second half to proceed further.
5. Return index value to main.
6. In main(), sequentially compare the remaining items of search sequence to next items in the
array.
7. Print the index range of the sequence found.
8. Exit.

CODE-
#include<iostream>
using namespace std;
int BinarySearch(int a[], int start, int end, int item, int iter)
{
int i, mid;
iter++;
mid = start + (end-start+1)/2;
if(item > a[end] || item < a[start] || mid == end)
{
cout<<"\nNot found";
return -1;
}
else if(item == a[mid])
{
return mid;
}
else if(item == a[start])
{
return start;
}
else if(item == a[end])
{
return end;
}
else if(item > a[mid])
BinarySearch(a, mid, 19, item, iter);
else
BinarySearch(a, start, mid, item, iter);
}
int main()
{
int n, i, flag=0, Bindex, a[20]={1, 9, 18, 24, 27, 35, 38, 41, 49, 53, 55, 66, 67, 72, 75, 77, 81, 89,
90, 97};
cout<<"---- Name : Rahul Roy, Section : CSE-3D, Roll No : 43 ----"<<endl;
cout<<"\nEnter the number of element in the search sequence: ";
cin>>n;
int s[n];
for(i = 0; i < n; i++)
cin>>s[i];
Bindex = BinarySearch(a, 0, 19, s[0], 0);
if(Bindex == -1)
{
cout<<"\nNot found.";
return 0;
}
else
{
for(i = Bindex; i < n+Bindex; i++)
if(a[i] != s[i-Bindex])
flag = 5;
if(flag == 5)
cout<<"\nNot found.";
else
cout<<"\nSequence found between index "<<Bindex<<" and
"<<Bindex+n<<".";
}
return 0;
}

OUTPUT-
7.Write a C++ program to implement Linear search using object as parameter.

ALGORITHM
1. Assign the data to the array in a sorted manner.
2. Call linearSearch() function with ‘arr’ the array of data and ‘n’ the number of values, start and end
index, iteration count and s[0] be the element to be searched in the argument list.
3. Increment the iteration counter and compare the item value with the a[mid].
4. If item < a[mid] choose first half otherwise second half to proceed further.
5. Return index value to main.
6. In main(), sequentially compare the remaining items of search sequence to next items in the
array.
7. Print the index range of the sequence found.
8. Exit.

CODE-
#include<iostream>
using namespace std;
int BinarySearch(int a[], int start, int end, int item, int iter)
{
int i, mid;
iter++;
mid = start + (end-start+1)/2;
if(item > a[end] || item < a[start] || mid == end)
{
cout<<"\nNot found";
return -1;
}
else if(item == a[mid])
{
return mid;
}
else if(item == a[start])
{
return start;
}
else if(item == a[end])
{
return end;
}
else if(item > a[mid])
BinarySearch(a, mid, 19, item, iter);
else
BinarySearch(a, start, mid, item, iter);
}
int main()
{
int n, i, flag=0, Bindex, a[20]={1, 9, 18, 24, 27, 35, 38, 41, 49, 53, 55, 66, 67, 72, 75, 77, 81, 89,
90, 97};
cout<<"---- Name : Rahul Roy, Section : CSE-3D, Roll No : 43 ----"<<endl;
cout<<"\nEnter the number of element in the search sequence: ";
cin>>n;
int s[n];
for(i = 0; i < n; i++)
cin>>s[i];
Bindex = BinarySearch(a, 0, 19, s[0], 0);
if(Bindex == -1)
{
cout<<"\nNot found.";
return 0;
}
else
{
for(i = Bindex; i < n+Bindex; i++)
if(a[i] != s[i-Bindex])
flag = 5;
if(flag == 5)
cout<<"\nNot found.";
else
cout<<"\nSequence found between index "<<Bindex<<" and
"<<Bindex+n<<".";
}
return 0;
}

OUTPUT-
8.Write a C++ program to implement Bubble Sort using object as parameter.

ALGORITHM
1. Take input of data.
2. Call BubbleSort() function with ‘arr’ the array of data and ‘n’ the number of values, in the
argument list.
3. Implement Sorting algorithm using nested for loop.
4. The first loop will run on ‘i’ from 0 to n-1.
5. The second loop will run on ‘j’ from 0 to n-i-1.
6. Compare two consecutive values.
7. Switch the values if arr[j+1] <arr[j].
8. Return to main and display the result.
9. Exit.

CODE-
#include <iostream>
using namespace std;
void BubbleSort (int arr[], int n)
{
int i, j;
for (i = 0; i < n; ++i)
{
for (j = 0; j < n-i-1; ++j)
{
if (arr[j] > arr[j+1])
{
arr[j] = arr[j]+arr[j+1];
arr[j+1] = arr[j]-arr[j + 1];
arr[j] = arr[j]-arr[j + 1];
}
}
}
}
int main()
{
int n, i;
cout<<"---- Name : Rahul Roy, Section : CSE-3D, Roll No : 43 ----"<<endl; cout<<"\nEnter the
number of data element to be sorted: ";
cin>>n;
int arr[n];
for(i = 0; i < n; i++)
{
cout<<"Enter element "<<i+1<<": ";
cin>>arr[i];
}
BubbleSort(arr, n);
cout<<"\nSorted Data ";
for (i = 0; i < n; i++)
cout<<"->"<<arr[i];
return 0;
}

OUTPUT-

9. Write a C++ program to implement swapping of two numbers using friend function.

CODE-
#include <iostream>
using namespace std;
class Swap {
int temp, a, b;
public:
Swap(int a, int b)
{
this->a = a;
this->b = b;
}
friend void swap(Swap&);
};
void swap(Swap& s1)
{
cout << "\nBefore Swapping: " << s1.a << " " << s1.b;
s1.temp = s1.a;
s1.a = s1.b;
s1.b = s1.temp;
cout << "\nAfter Swapping: " << s1.a << " " << s1.b;
}
int main()
{
cout<<"---- Name : Rahul Roy, Section : CSE-3D, Roll No : 43 ----"<<endl;
Swap s(4, 6);
swap(s);
return 0;
}

OUTPUT-

10. Write a C++ program to get the area of square and rectangle using friend class concept. It
should include the concept of constructor also.

CODE-
#include <iostream>
using namespace std;
class Square
{
friend class Rectangle;
int side;
public:
Square ( int s )
{
side = s;
}
};
class Rectangle
{
int length;
int breadth;

public:
int getArea()
{
return length * breadth;
}
void shape( Square a )
{
length = a.side;
breadth = a.side;
}
};
int main()
{
cout<<"---- Name : Rahul Roy, Section : CSE-3D, Roll No : 43 ----"<<endl;
Square square(5);
Rectangle rectangle;
rectangle.shape(square);
cout << rectangle.getArea() << endl;
return 0;
}

OUTPUT-

You might also like