Thanks to visit codestin.com
Credit goes to www.scribd.com

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

DS Lab-3

The document contains code implementations for three sorting algorithms: Bubble Sort, Selection Sort, and Insertion Sort. Each algorithm prompts the user to enter the size and elements of an array, sorts the array using its respective method, and then displays the sorted array. The code is written in C++ and demonstrates basic sorting techniques.

Uploaded by

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

DS Lab-3

The document contains code implementations for three sorting algorithms: Bubble Sort, Selection Sort, and Insertion Sort. Each algorithm prompts the user to enter the size and elements of an array, sorts the array using its respective method, and then displays the sorted array. The code is written in C++ and demonstrates basic sorting techniques.

Uploaded by

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

1. Bubble Sort.

Code:
#include <iostream>

using namespace std;

int main()

int n, i, arr[100], j, temp;

cout << "Enter the Size of Array: ";

cin >> n;

cout << "Enter " << n << " Elements: " << endl;

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

cin >> arr[i];

for (i = 0; i < (n - 1); i++)

for (j = 0; j < (n - i - 1); j++)

if (arr[j] > arr[j + 1])

temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

cout << "\nArray Sorted Successfully!\n";

cout << "\nThe New Sorted Array is: \n";

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

cout << arr[i] << " ";

cout << endl;

return 0;

}
2. Selection Sort.
Code:
#include <iostream>

using namespace std;

int main()

int total, arr[100], i, j, temp, small, check, index;

cout << "Enter the Size of Array: ";

cin >> total;

cout << "Enter " << total << " Array Elements: " << endl;

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

cin >> arr[i];

for (i = 0; i < (total - 1); i++)

check = 0;

small = arr[i];

for (j = (i + 1); j < total; j++)

if (small > arr[j])

small = arr[j];

check++;

index = j;

if (check != 0)

temp = arr[i];

arr[i] = small;

arr[index] = temp;
}

cout << "\nThe Sorted Array is:\n";

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

cout << arr[i] << " ";

cout << endl;

return 0;

}
3. Insertion Sort.
Code:
#include <iostream>

using namespace std;

int main()

int arr[100], total, i, j, k, element, index;

cout << "Enter the Size of Array: ";

cin >> total;

cout << "Enter " << total << " Array Elements: " << endl;

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

cin >> arr[i];

for (i = 1; i < total; i++)

element = arr[i];

if (element < arr[i - 1])

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

if (element < arr[j])

index = j;

for (k = i; k > j; k--)

arr[k] = arr[k - 1];

break;

else

continue;
arr[index] = element;

cout << "\nThe New Sorted Array is:\n";

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

cout << arr[i] << " ";

cout << endl;

return 0;

You might also like