Professional Documents
Culture Documents
DATASTRUCTURE CODE ASSIGNMENT n12346
DATASTRUCTURE CODE ASSIGNMENT n12346
NAME IDNO
SAGNE MISGANA ……………….
TOLERA YADATA…………………….4753
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 inplace 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>
int n = arr.size();
int j = i - 1;
arr[j + 1] = arr[j];
j = j - 1;
arr[j + 1] = key;
int main() {
int numNames;
cout << "Enter the number of names want: ";
vector<string> names;
string name;
names.push_back(name);
insertionSort(names);
return 0;
#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;: 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::.
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++) {: This line starts a loop from index 1 to the end of the vector
arr.
// string key = arr[i];: This line stores the value of arr at index i in the string variable key.
int j = i - 1;
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;
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;
// cout << "Enter the number of names: ";: This line displays the message "Enter the number of
names: " on the console.
//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++) {: This line starts a for loop from 0 to numNames - 1.
// cout << "Enter name " << i + 1 << ": ";: This line displays the message "Enter name <i +
1>: " on the console.
//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;: This line displays the message "Sorted
names in ascending order: " on the console.
//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;: This line displays the value at index i in the names vector on
the console followed by a new line.
}
return 0;