0% found this document useful (0 votes)
48 views12 pages

Insertion Sort

The document provides a detailed explanation of insertion sort, including its implementation in C++ and an example of sorting an array. It also discusses the structure and advantages of using linked lists, along with sample code for inserting elements into a singly linked list. Additionally, it outlines the process of sorting an array using insertion sort and compares the original and sorted arrays.

Uploaded by

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

Insertion Sort

The document provides a detailed explanation of insertion sort, including its implementation in C++ and an example of sorting an array. It also discusses the structure and advantages of using linked lists, along with sample code for inserting elements into a singly linked list. Additionally, it outlines the process of sorting an array using insertion sort and compares the original and sorted arrays.

Uploaded by

Agrippa Mungazi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#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;

// Move elements greater than key to one position ahead

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

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

j--;

arr[j + 1] = key;

// Utility function to print the array

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

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

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

cout << endl;

// Example usage

int main() {
int arr[] = {12, 11, 13, 5, 6};

int n = sizeof(arr) / sizeof(arr[0]);

cout << "Original array:\n";

printArray(arr, n);

insertionSort(arr, n);

cout << "Sorted array:\n";

printArray(arr, n);

return 0;

In insertion sort, the number of passes is always n – 1, where n is the number


of elements in the array.

Why?

Each pass starts at index I = 1 and goes up to I = n – 1.

On each pass, the element at index I is inserted into the correct position
among the previous I elements (which are already sorted).

Example:
For the array: [12, 11, 13, 5, 6]

Number of elements n = 5

Number of passes = 5 – 1 = 4

Each pass:

Pass 1: Insert 11 into [12] → [11, 12]

Pass 2: Insert 13 into [11, 12] → [11, 12, 13]

Pass 3: Insert 5 into [11, 12, 13] → [5, 11, 12, 13]

Pass 4: Insert 6 into [5, 11, 12, 13] → [5, 6, 11, 12, 13]

Do you want to see a step-by-step trace of one example?

[5/7, 03:21] ChatGPT: Great, this is a well-structured Computer Science paper


involving data structures and algorithms. Here’s how you can address the
tasks from *Question Two* and *Three*:

*QUESTION TWO*

a. Linked List Diagrams (sketched logically):

*i. Singly Linked List:*

```
[23 | *] → [12 | *] → [21 | *] → [6 | *] → [3 | *] → [5 | *] → [7 | *] → [1 | *] → [16
| *] → [15 | NULL]

```

*ii. Doubly Linked List:*

```

NULL ← [23 | * | *] ↔ [12 | * | *] ↔ [21 | * | *] ↔ … ↔ [15 | * | NULL]

```

*iii. Circular Linked List:*

```

[23 | *] → [12 | *] → … → [15 | *] ┘

↑__________________┘

```

b. *C++ Code to Insert into a Singly Linked List*

```cpp

#include <iostream>

Using namespace std;

Struct Node {

Int data;

Node* next;

};
Void insert(Node*& head, int value) {

Node* newNode = new Node{value, nullptr};

If (!head) {

Head = newNode;

} else {

Node* temp = head;

While (temp->next) temp = temp->next;

Temp->next = newNode;

Void display(Node* head) {

While (head) {

Cout << head->data << “ -> “;

Head = head->next;

Cout << “NULL” << endl;

Int main() {

Int data[] = {23, 12, 21, 6, 3, 5, 7, 1, 16, 15};

Node* head = nullptr;

For (int I = 0; I < 10; i++) {

Insert(head, data[i]);

[5/7, 03:21] ChatGPT: }

Display(head);
Return 0;

```

c. *Why use data structures instead of normal variables?*

- *Efficient memory usage*: Data structures like arrays, linked lists, etc.,
allow storing multiple values efficiently.

- *Better data management*: They support dynamic storage and retrieval


operations.

- *Algorithm implementation*: Structures like stacks, queues, trees help


implement complex algorithms.

- *Code reusability and scalability*: Easier to modify, expand, and reuse.

- *Improved performance*: Operations like search, sort, and traversal are


optimized using appropriate data structures.

*QUESTION THREE*

a. *Insertion Sort Explanation (with given array)*:

*Initial array*:

`23 12 21 6 3 5 7 1 16 15`

*Process*:

- Step 1: 12 < 23 → Insert 12 before 23 → `12 23`

- Step 2: 21 < 23 → Insert 21 between 12 and 23 → `12 21 23`

- Step 3: 6 → insert at front → `6 12 21 23`

- Step 4: 3 → insert at front → `3 6 12 21 23`


- Step 5: 5 → insert after 3 → `3 5 6 12 21 23`

- Step 6: 7 → after 6 → `3 5 6 7 12 21 23`

- Step 7: 1 → insert at front → `1 3 5 6 7 12 21 23`

- Step 8: 16 → between 12 and 21 → `1 3 5 6 7 12 16 21 23`

- Step 9: 15 → between 12 and 16 → `1 3 5 6 7 12 15 16 21 23`

*Final sorted*: `1 3 5 6 7 12 15 16 21 23`

b. *C++ Code for Insertion Sort*

```cpp

[5/7, 03:21] ChatGPT: #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;

// Move elements greater than key

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

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

j--;

Arr[j + 1] = key;

}
Int main() {

Int myArray[] = {23, 12, 21, 6, 3, 5, 7, 1, 16, 15};

Int size = sizeof(myArray) / sizeof(myArray[0]);

insertionSort(myArray, size);

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

cout << myArray[i] << “ “;

Return 0;

```

Would you like help with *Question Four* (binary tree and traversal)?

3b

Sure! Here’s the answer to Question 3b from your image:

Question 3b:

Assume the array above is called myArray. Write the code for sorting it using
insertion sort.

Array: myArray = {6, 3, 12, 5, 1, 7, 15, 16}


C++ Code Using Insertion Sort:

#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;

// Move elements greater than key to one position ahead

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

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

j--;

Arr[j + 1] = key;

// Function to print array

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

For (int I = 0; I < n; ++i)

Cout << arr[i] << “ “;

Cout << endl;


}

Int main() {

Int myArray[] = {6, 3, 12, 5, 1, 7, 15, 16};

Int size = sizeof(myArray) / sizeof(myArray[0]);

Cout << “Original array: “;

printArray(myArray, size);

insertionSort(myArray, size);

cout << “Sorted array: “;

printArray(myArray, size);

return 0;

This code sorts the array using insertion sort and displays both the original
and sorted versions.

Would you like the step-by-step output for each pass as well?

Thanks for catching that! Here's the correct answer to Question 2b, which asks:
Question 2b: Write C++ code for inserting data items in a singly
linked list.

Answer:

Below is simple C++ code to insert data items into a singly linked list at the end:

#include <iostream>
using namespace std;

// Node structure
struct Node {
int data;
Node* next;
};

// Function to insert at the end


void insert(Node*& head, int value) {
Node* newNode = new Node{value, nullptr};

if (head == nullptr) {
head = newNode;
} else {
Node* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newNode;
}
}

// Function to display the list


void display(Node* head) {
Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
}

// Main function
int main() {
Node* head = nullptr;

// Insert data items


insert(head, 10);
insert(head, 20);
insert(head, 30);

// Display the list


display(head);

return 0;
}

This code:

 Defines a Node structure for a singly linked list


 Uses insert() to add elements at the end
 Uses display() to print the list

Would you like the version that inserts at the beginning or in sorted order instead?

You might also like