DATASTRUCTURE CODE ASSIGNMENT N GROUP 7

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

MATTU UNIVERSITY

COLLEGE OF ENGINNEERING AND TECHNOLOGY

DEPARTMENT OF COMPUTER SCIENCE


DATASTRUCTURE CODE ASSIGNMENT 1

NAME IDNO
SAGNE MISGANA ……………….4753

TOLERA YADATA…………………….4759

AMANUEL MARSHA………………….4766

GETACHEW ADUGNA……………….1241

CHERINAT ASRAT……………………0753

ANTENEH TASFAYE………………….5199
Insertion Sort :
ISit inserts each item into its proper place in
the final list. The simplest implementation of this requires two list structures - the source list and
the list into which sorted items are inserted. To save memory, most implementations use an in
place sort that works by moving the current item past the already sorted items and repeatedly
swapping it with the preceding item until it is in place.

//DATASTRUCTURE CODE ASSIGNMENT 1

#include <iostream>

#include <vector>

#include <string>

using namespace std;

void insertionSort(vector<string>& arr) {

int n = arr.size();

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

string key = arr[i];

int j = i - 1;

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

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

j = j - 1;

arr[j + 1] = key;

int main() {

int numNames;
cout << "Enter the number of names want: ";

cin >> numNames;

vector<string> names;

string name;

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

cout << "Enter name " << i + 1 << ": ";

cin >> name;

names.push_back(name);

insertionSort(names);

cout << "Sorted names in ascending order: " << endl;

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

cout << names[i] << endl;

return 0;

//DATASTRUCTURE CODE ASSIGNMENT 1

#include <iostream>

//#include <iostream>: This line includes the input/output stream library, which allows us to
perform input and output operations in C++.
#include <vector>

//#include <vector>: This line includes the vector library, which allows us to use vectors in C++
to store and manipulate dynamic arrays.

#include <string>

//#include <string>: This line includes the string library, which allows us to work with strings in
C++.

using namespace std;

//using namespace std;: This line tells the compiler to use the standard namespace for all the
libraries we have included,

// which means that we can access standard C++ functions and objects without explicitly
qualifying them with std::.

void insertionSort(vector<string>& arr) {

//void insertionSort(vector<string>& arr) {: This line defines a function named


insertionSort that takes a vector of strings as a reference parameter.

int n = arr.size();

//int n = arr.size();: This line gets the size of the vector arr and stores it in the integer variable
n.

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

// for (int i = 1; i < n; i++) {: This line starts a loop from index 1 to the end of the vector
arr.

string key = arr[i];

// string key = arr[i];: This line stores the value of arr at index i in the string variable key.

int j = i - 1;

// int j = i - 1;: This line initializes the variable j to be i - 1.

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


// while (j >= 0 && arr[j] > key) {: This line starts a while loop that runs as long as j is
greater than or equal to 0 and the value of arr at index j is greater than key.

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

//arr[j + 1] = arr[j];: This line shifts the value at index j in arr to the right by one position.

j = j - 1;

// j = j - 1;: This line decrements the value of j by 1.

arr[j + 1] = key;

//arr[j + 1] = key;: This line assigns the value of key to the position at index j + 1 in arr.

int main() {

// int main() {: This line defines the main function, which is the entry point of the
program.

int numNames;

//int numNames;: This line declares an integer variable numNames.

cout << "Enter the number of names: ";

// cout << "Enter the number of names: ";: This line displays the message "Enter the number of
names: " on the console.

cin >> numNames;

//cin >> numNames;: This line takes input from the user and stores it in the variable
numNames.

vector<string> names;

// vector<string> names;: This line declares a vector named names to store strings.

string name;
//string name;: This line declares a string variable name.

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

//for (int i = 0; i < numNames; i++) {: This line starts a for loop from 0 to numNames - 1.

cout << "Enter name " << i + 1 << ": ";

// cout << "Enter name " << i + 1 << ": ";: This line displays the message "Enter name <i +
1>: " on the console.

cin >> name;

//cin >> name;: This line takes input from the user and stores it in the variable name.

names.push_back(name);

// names.push_back(name);: This line adds the value of name to the end of the vector
names.

insertionSort(names);

// insertionSort(names);: This line calls the insertionSort function with names as the argument.

cout << "Sorted names in ascending order: " << endl;

//cout << "Sorted names in ascending order: " << endl;: This line displays the message "Sorted
names in ascending order: " on the console.

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

//for (int i = 0; i < names.size(); i++) {: This line starts a for loop from 0 to the size of the
vector names - 1.

cout << names[i] << endl;

//cout << names[i] << endl;: This line displays the value at index i in the names vector on
the console followed by a new line.

}
return 0;

You might also like