Technical Report: Insertion Sort
Implementation in C# and C++
Ministry of Higher Education
Higher Institute for Engineering and Technology – Bhihra
Department: [Department Name]
Title: Insertion Sort Algorithm Implementation Using C# and C++ (13pt, Bold, 1.5 spacing)
Submission Note: "A technical report submitted to the [...] Department in partial fulfillment
of the requirements for the assessment of term work in Technical Report Writing Course."
Student Names/Numbers: [Name 1 & Number], [Name 2 & Number]
Supervisor: [Title & Name]
Location/Date: Bhihra, Egypt, [Date]
Table of Contents
Dedication
Acknowledgments
List of Tables
List of Figures
Abstract
Introduction
Materials and Methods
Results and Discussion
Conclusion
References
Appendices
Dedication
To my parents and family who made this accomplishment possible.
Acknowledgments
Thanks to my professors, advisors, and my family for their unwavering support and
guidance throughout the completion of this project.
List of Tables
Table 3.1 - Original and Sorted Arrays ...... 14
List of Figures
Figure 6.1 - Flowchart of Insertion Sort ...... 12
Glossary
IDE: Integrated Development Environment
O(n^2): Quadratic Time Complexity
List of Symbols and Abbreviations
IDE: Integrated Development Environment
C#: C Sharp Programming Language
C++: C Plus Plus Programming Language
Abstract
Sorting is a fundamental operation in computer science, necessary for efficient data
organization and retrieval. This report investigates the Insertion Sort algorithm, known for
its simplicity and intuitive process. The algorithm was implemented in both C# and C++
programming languages to compare performance and understand the differences in syntax
and behavior. Experimental results showed that while Insertion Sort performs well on small
datasets, its quadratic time complexity makes it inefficient for larger data. Between the two
implementations, C++ provided slightly faster execution times.
Keywords: Insertion Sort, Sorting Algorithms, C#, C++, Time Complexity
Introduction
1.1 Background
Sorting algorithms are essential building blocks in the field of computer science and
software engineering. They are critical in tasks ranging from database optimization to
efficient data visualization. Among the many sorting algorithms available, Insertion Sort
stands out for its straightforward logic, making it an ideal choice for educational purposes
and small data sets.
1.2 Problem Statement
Although powerful sorting algorithms like Quick Sort and Merge Sort are widely used, their
complexity can be overwhelming for beginners. A simple and understandable sorting
method like Insertion Sort can bridge this learning gap.
1.3 Objective
This report aims to:
- Implement Insertion Sort in C# and C++.
- Explain each step of the algorithm.
- Compare the performance between the two languages on small datasets.
1.4 Literature Review
Insertion Sort has been a cornerstone example in computer science education for decades.
Its ability to efficiently handle small datasets and nearly sorted data makes it a practical
choice in certain applications despite its O(n^2) average and worst-case time complexity.
Materials and Methods
2.1 Programming Languages
- C#: Implemented using Microsoft Visual Studio.
- C++: Implemented using Code::Blocks and Visual Studio.
2.2 Insertion Sort Algorithm
Insertion Sort builds the final sorted array one item at a time:
- Start from the second element.
- Compare it to previous elements.
- Insert it into the correct position.
2.3 C# Implementation
using System;
class Program {
static void InsertionSort(int[] array) {
for (int i = 1; i < array.Length; i++) {
int key = array[i];
int j = i - 1;
while (j >= 0 && array[j] > key) {
array[j + 1] = array[j];
j = j - 1;
}
array[j + 1] = key;
}
}
static void Main() {
int[] arr = { 5, 2, 9, 1, 5, 6 };
Console.WriteLine("Original Array:");
Console.WriteLine(string.Join(", ", arr));
InsertionSort(arr);
Console.WriteLine("Sorted Array:");
Console.WriteLine(string.Join(", ", arr));
}
}
2.4 C++ Implementation
#include <iostream>
using namespace std;
void insertionSort(int array[], int size) {
for (int i = 1; i < size; i++) {
int key = array[i];
int j = i - 1;
while (j >= 0 && array[j] > key) {
array[j + 1] = array[j];
j = j - 1;
}
array[j + 1] = key;
}
}
int main() {
int arr[] = { 5, 2, 9, 1, 5, 6 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Original Array: ";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
insertionSort(arr, n);
cout << "Sorted Array: ";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
return 0;
}
Results and Discussion
Both C# and C++ implementations successfully sorted the sample arrays. C++ demonstrated
slightly faster execution time. The following table illustrates the sorting results:
| Original Array | Sorted Array |
|----------------|--------------|
| 5, 2, 9, 1, 5, 6 | 1, 2, 5, 5, 6, 9 |
Insertion Sort is simple and effective for small datasets but inefficient for large ones.
Conclusion
Insertion Sort is easy to understand and implement, making it ideal for educational
purposes. For small or nearly sorted datasets, it performs adequately. However, for larger
datasets, more efficient algorithms are recommended. Both C# and C++ were effective for
this task.
References
- [1] Cormen, T. H., Leiserson, C. E., Rivest, R. L., & Stein, C. (2009). Introduction to
Algorithms (3rd ed.). MIT Press.
- [2] Microsoft Documentation - C# Programming Guide
- [3] ISO C++ Foundation - C++ Standard Documentation
Appendices
- Full Source Code for C# and C++ Implementations
- Flowchart Diagram of Insertion Sort