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

Singly Selection List

Data structure

Uploaded by

xhpxrwyzhx
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)
5 views

Singly Selection List

Data structure

Uploaded by

xhpxrwyzhx
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
You are on page 1/ 10

INSTITUTE OF ACCOUNTANCY ARUSHA (IAA)

GROUP NUMBER 4

MODULE NAME : DATA STRUCTURE AND ALGORITHMS

PROGRAMME : BCS III

CODE : ITU 08209

FACILITATOR : Almas (Mr. Kabeya)

ACADEMIC YEAR : 2023/2024

SEMESTER : TWO

GROUP FIVE

N NAME REGISTRATION NUMBER SIGNATURE


O
1 ALBANUS MASSAWE BCS/0011/2021
2 FABIAN DAUD SIMON BCS/0088/2021
3 BARAKA ABUBAKARI BCS/0008/2021
4 ALOYCE MAGOGWA BCS/0057/2021
5 OSCAR KLYIEN BCS/0080/2021
6 SILIVIA MAFURU BCS/0109/2021
7 HUMPREY BCS/0037/2021
8 JANETH JAMES KIDEGEYE BCS/0105/2021
9 CHARLES MARTIN BCS/0121/2021
10 DEUSI MALIGANYA BCS/0089/2021
SORTING ALGORITHMS

are a set of techniques used to rearrange elements of a list or an array in a specific order. The
most common orderings are numerical or lexicographical (alphabetical). Sorting is a fundamental
operation in computer science and is used in various applications, including searching, data
compression, and data analysis

Types of the sorting algorithms:

There are numerous sorting algorithms, each with its own advantages and disadvantages in terms
of performance, stability, and ease of implementation. Some of the most well-known sorting
algorithms include:

a) Insertion Sort:

Insertion sort builds the final sorted list one item at a time by repeatedly taking the next element
from the unsorted part and inserting it into its correct position in the sorted part
Advantages of the insertion sort

 Its simple and easy to understand and implement.

 Efficient for small data sets or nearly sorted data.

 in-place srting algorithms, meaning it doesn’t require extra memory.

Dis-Advantages of the insertion sort

 Insertion sort has a lot of swaps which can make it slow on Morden computers

 Inefficient for large data sets or reverse-ordered data with a worst-case time complexity

Example of insertion sort in java

public class InsertionSort {

// Function to sort an array using insertion sort

public static void insertionSort(int[] arr) {

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

int key = arr[i];

int j = i - 1;

// Move elements of arr[0..i-1], that are greater than key,

// to one position ahead of their current position

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

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

j = j - 1;

arr[j + 1] = key;

// Function to print the sorted array

public static void printArray(int[] arr) {

int n = arr.length;

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

System.out.print(arr[i] + " ");

System.out.println();

// Main method to test the insertion sort algorithm

public static void main(String[] args) {

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

System.out.println("Original array:");
printArray(arr);

insertionSort(arr);

System.out.println("Sorted array:");

printArray(arr);

b) Bubble sort
is a simple sorting algorithm that repeatedly steps through the list, compares adjacent
elements, and swaps them if they are in the wrong order. The pass through the list is
repeated until the list is sorted. It's called "Bubble Sort" because smaller elements
gradually "bubble" to the top of the list.
 Due to that the folowings are the advantages and the dis-advantages of the bubble sort

Dis-advantages of bubble sort


 Inefficient for large datasets: Bubble Sort has a time complexity of O(n^2), making it
highly inefficient for sorting large datasets. Its performance degrades significantly as the
number of elements increases.
 Poor performance even for nearly sorted lists: Bubble Sort's time complexity remains
O(n^2) even if the input array is almost sorted. This makes it unsuitable for scenarios
where the input is likely to be partially sorted.
 Not suitable for real-world applications: Due to its poor performance for large datasets,
Bubble Sort is rarely used in real-world applications where efficiency is crucial. Other
sorting algorithms like Merge Sort, Quick Sort, or Heap Sort are preferred in such cases.

Example of java code


public class BubbleSort {
public static void bubbleSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j+1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp; }
} } }
public static void main(String[] args) {
int[] arr = {64, 34, 25, 12, 22, 11, 90};
System.out.println("Original array:");
printArray(arr);
bubbleSort(arr);
System.out.println("Sorted array:");
printArray(arr); }
public static void printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " "); }
System.out.println(); }}

c) Merge Sort
is a divide-and-conquer algorithm used for sorting arrays (or lists).
the merge sort it works as follows:

 Divide:
The array is divided into two (or more) subarrays recursively until each subarray
contains only one element. This process continues until the base case is reached, i.e., the
subarrays contain only one element each.
 Conquer:
The divided subarrays are then merged back together in a sorted manner. This merging
process occurs recursively, combining two smaller sorted arrays (subarrays) into a larger
sorted array until the entire array is sorted.
 Merge:
During the merge step, the two subarrays are merged together into a single sorted array.
This is done by comparing the elements of the two subarrays one by one and selecting the
smaller (or larger) element first. The selected element is then placed into a new array.
This process continues until all elements from both subarrays are merged into the new
array.
Advantages of the merge sort

 Guaranteed performance:
Merge Sort has a time complexity of O(n log n) in all cases, making it more efficient than
sorting algorithms like Bubble Sort or Insertion Sort, especially for large datasets.
 Stable sorting:
Merge Sort is stable, meaning it preserves the relative order of equal elements in the
sorted array.

Advantages continue………

 Parallelizable:
Merge Sort can take advantage of parallelism, as the merging of sorted subarrays can be
done independently.
Example of java code in merge sort
public class MergeSort {
public static void mergeSort(int[] arr) {
if (arr == null || arr.length < 2) return;
sort(arr, 0, arr.length - 1); }
private static void sort(int[] arr, int left, int right) {
if (left < right) {
int mid = (left + right) / 2;
// Sort first and second halves
sort(arr, left, mid);
sort(arr, mid + 1, right);
// Merge the sorted halves
merge(arr, left, mid, right); } }
private static void merge(int[] arr, int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;
// Create temporary arrays
int[] L = new int[n1];
int[] R = new int[n2];
// Copy data to temporary arrays
for (int i = 0; i < n1; ++i)
L[i] = arr[left + i];
for (int j = 0; j < n2; ++j)
R[j] = arr[mid + 1 + j];
// Merge the temporary arrays
int i = 0, j = 0;
int k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++; } else {
arr[k] = R[j];
j++; }
k++;
}
// Copy remaining elements of L[] if any
while (i < n1) {
arr[k] = L[i];
i++;
k++; }
// Copy remaining elements of R[] if any
while (j < n2) {
arr[k] = R[j];
j++;
k++; } }
public static void main(String[] args) {
int[] arr = {12, 11, 13, 5, 6, 7};
System.out.println("Original array:");
printArray(arr);
mergeSort(arr);
System.out.println("Sorted array:");
printArray(arr); }
public static void printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " "); }
System.out.println(); }}

You might also like