Lab 9
Lab 9
Objective(s):
Upon completion of this lab session, learners will be able to:
Implementation of Insertion Sort
Lab Tasks:
Task #.1: Estimated Time: 30 Mins.
Write a program in C++ to implement Bubble Sort for sorting ‘n’ number of elements
in an array.
#include <iostream>
Using namespace std;
Void bubbleSort(int arr[], int n) {
For (int I = 0; I < n – 1; ++i) {
For (int j = 0; j < n – I – 1; ++j) {
If (arr[j] > arr[j + 1]) {
Int main() {
Int n;
Int arr[n];
bubbleSort(arr, n);
Return 0;
}
Page 2 of 8
%
Enrollment Number: ____________________________
#include <iostream>
Using namespace std;
Class Node {
Public:
Int data;
Node* next;
Return;
}
Bool swapped;
Node* ptr1;
Node* lptr = nullptr;
Do {
Swapped = false;
Ptr1 = start;
Int main() {
Page 3 of 8
%
Enrollment Number: ____________________________
Node* head = new Node(5);
Head->next = new Node(2);
Head->next->next = new Node(9);
Head->next->next->next = new Node(1);
Head->next->next->next->next = new Node(7);
bubbleSortLinkedList(head);
return 0;
}
#include <iostream>
Using namespace std;
Void selectionSort(int arr[], int n) {
For (int I = 0; I < n – 1; ++i) {
Int minIndex = I;
For (int j = I + 1; j < n; ++j) {
If (arr[j] < arr[minIndex]) {
minIndex = j;
}
Page 4 of 8
%
Enrollment Number: ____________________________
}
Int main() {
Int n;
Int arr[n];
selectionSort(arr, n);
Return 0;
}
Page 5 of 8
%
Enrollment Number: ____________________________
#include <iostream>
Using namespace std;
Void insertionSort(int arr[], int n) {
For (int I = 1; I < n; ++i) {
Int key = arr[i];
Int j = I – 1;
Arr[j + 1] = key;
}
}
Int main() {
Int n;
Page 6 of 8
%
Enrollment Number: ____________________________
Cin >> n;
Int arr[n];
insertionSort(arr, n);
Return 0;
}
Page 7 of 8
%
Enrollment Number: ____________________________
Max
Obtained
Task Mark Comments(if any)
Marks
s
1. 10
2. 10
3. 10
4. 10
Total 10 Signature
Note : Attempt all tasks and get them checked by your Lab Instructor.
Page 8 of 8