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

Data Structure

This document contains code for three sorting algorithms: bubble sort, selection sort, and insertion sort. For each algorithm, the code includes a function to implement the sorting logic, a function to print the sorted array, and a main function to accept user input for the array size and elements, call the sorting function, and print the results. The document is submitted by Mulatie Kindie as an individual assignment on data structures and algorithms.

Uploaded by

Tewodros Derese
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Data Structure

This document contains code for three sorting algorithms: bubble sort, selection sort, and insertion sort. For each algorithm, the code includes a function to implement the sorting logic, a function to print the sorted array, and a main function to accept user input for the array size and elements, call the sorting function, and print the results. The document is submitted by Mulatie Kindie as an individual assignment on data structures and algorithms.

Uploaded by

Tewodros Derese
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

ARBA MINCH

INSTITUTE OF TECHNOLOGY
UNIVERSITY

DEPARTMENT OF SOFTWARE ENGINEERING

DATA structure and algorithm ASSIGNMENT

Name: Mulatie Kindie


ID: NSR/1779/13
Section: B

Submitted to: mister.


Date: June 2023

DATA structure and algorithm INDIVIDUAL ASSIGNMENT


#include <iostream>
Bubble sorting on my editor and word format 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 temp = arr[j];

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

arr[j + 1] = temp ; }

void printArray(int arr[], int n) {

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

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

cout << endl;}

int main() {

int size;

cout << "Enter the size of the array: ";

cin >> size;

int arr[size];

cout << "Enter the elements of the array: ";

for (int i = 0; i < size; ++i) {

cin >> arr[i]; }

bubbleSort(arr, size);

cout << "Array after sorting: ";

printArray(arr, size);

return 0;

}
1

Mulatie Kindie
DATA structure and algorithm INDIVIDUAL ASSIGNMENT

#include <iostream>
Selection sorting on my editor and word format using namespace std;

void selectionSort(int arr[], int size) {

for (int i = 0; i < size - 1; i++) {

int minIndex = i;

for (int j = i + 1; j < size; j++) {

if (arr[j] < arr[minIndex]) {

minIndex = j;

int temp = arr[i];

arr[i] = arr[minIndex];

arr[minIndex] = temp;

int main() {

int size;

cout << "Enter the size of the array: ";

cin >> size;

int arr[size];

cout << "Enter the elements of the array:\n";

for (int i = 0; i < size; i++) {

cin >> arr[i]; }

selectionSort(arr, size);

cout << "Sorted array: ";

for (int i = 0; i < size; i++) {

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

cout << endl;

return 0;

}
1

Mulatie Kindie
DATA structure and algorithm INDIVIDUAL ASSIGNMENT

Insertion sorting on my editor and word format #include <iostream>

using namespace std;

void insertionSort(int arr[], int size) {

for (int i = 1; i < size; i++) {

int key = arr[i];

int j = i - 1;

while (j >= 0 && arr[j] > key) {

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

j--;

arr[j + 1] = key;

int main() {

int size;

cout << "Enter the size of the array: ";

cin >> size;

int arr[size];

cout << "Enter the elements of the array: ";

for (int i = 0; i < size; i++) {

cin >> arr[i];

insertionSort(arr, size);

cout << "Sorted array: ";

for (int i = 0; i < size; i++) {

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

return 0;

}
1

Mulatie Kindie

You might also like