×

Search anything:

# Time and Space Complexity of Selection Sort on Linked List

#### Time Complexity Algorithms

Get this book -> Problems on Array: For Interviews and Competitive Programming

In this article, we will learn about the space and time complexity of the Merge sort algorithm on Linked List using Mathematical analysis of various cases.

1. Time Complexity Analysis of Selection Sort on Linked List
2. Worst Case Time Complexity Analysis
3. Best Case Time Complexity Analysis
4. Average Case Time Complexity Analysis
5. Space Complexity Analysis
6. Comparison between Selection Sort on Array and Linked list
7. Summarization

## C++ implementation of Selection Sort on Linked List

#### Code:

``````#include<bits/stdc++.h>
using namespace std;

// Class to define Node
class Node {
public:
int data;
Node* next;

Node(int data) {
this->data = data;
next = NULL;
}
};

//Function to implement Selection sort
void selectionSort(Node* head) {

//Traverse all over the List

Node* min  = thead;
Node* temp = thead->next;

//Find the smallest element in  the remaining List.
while(temp) {

if(min->data > temp->data) {
min = temp;
}
temp = temp->next;
}

//Swapping Smallest
int t = thead->data;
min->data = t;

}
}

int main() {

//Linked List : 3 1 2 11 9 6
Node* head = new Node(3);
Node* temp = head;
temp->next = new Node(1);
temp = temp->next;
temp->next = new Node(2);
temp = temp->next;
temp->next = new Node(11);
temp = temp->next;
temp->next = new Node(9);
temp = temp->next;
temp->next = new Node(6);
temp = temp->next;

//Output
}
cout<<endl;

return 0;
}
``````

#### Output:

``````Sorted Linked List : 1 2 3 6 9 11
``````

## Time Complexity Analysis of Selection Sort on Linked List

Let the Size of unsorted Linkedlist be n.

``````    unsorted Linkedlist: 3 1 2 5 4 6

Step 1:    | 3 1 2 5 4 6   0 -> N
Step 2:    1 | 3 2 5 4 6   1 -> N-1
Step 3:    1 2 | 3 5 4 6   2 -> N-2
Step 4:    1 2 3 | 5 4 6      .
Step 5:    1 2 3 4 | 7 6      .
Step 6:    1 2 3 4 6 | 7      1

``````

Step 1: There is no sorted element. To make the first element sorted we traverse the whole LinkedList of size N.
Step 2: For sorting the second element we travel the Linkedlist of size N-2.
Step 3: For sorting the third element we travel the Linkedlist of size N-3.
....
Step N-1: For sorting the (N-1)th element we travel the Linkedlist of size 1.
Step N: The last element is already sorted.

Time taken to sort n elements i.e.
T(n) = k(n) + k(n-1) + k(n-2) + ... + k(2) + k(1) , where k is constant which means some constant work is done while processing every step.
T(n) = k{n + (n-1) + (n-2) + ... + 2 + 1}
T(n) = kn(n-1) / 2;
T(n) = kn^2/2 - kn/2
Hence total time complexity : Î¸(n^2)

#### Worst Case Time Complexity Analysis

Worst Time Complexity occur when the Linkedlist is completely unsorted(or sorted in descending order).
E.g: 5 4 3 2 1
For every smallest node it has to travel to the extreme. The complexity remains the same.
Hence, Worst Case Time Complexity : O(n^2)

#### Best Case Time Complexity Analysis

Worst Time Complexity occur when the Linkedlist is already sorted(or sorted in increasing order).
E.g: 1 2 3 4 5
Yet the List is already sorted, it will search for the smallest element in the same manner explained. So, it's complexity will not change.
Hence, Best Case Time Complexity : Î©(n^2)

#### Average Case Time Complexity Analysis

It's the case when the Linkedlist list is neither in increasing order or decreasing order i.e all the elements are jumbled.
E.g: 5 1 3 4 2
And the no. operations to find the elements remains same. So, there is no change in it's complexity.
Hence, Average Case Time Complexity : Î¸(n^2)

## Space Complexity Analysis

Space Complexity: O(1) i.e constant space.
As it doesn't require any extra space to sort the List. It itself sort the List in the given Linkedlist.

## Comparison between Selection Sort on Array and Linked list

The time complexity of Selection Sort is same on both Array and Linked List as:

• The major disadvantage of Linked List over array is lack of random access.
• This limitation does not play any role as Selection Sort involves only sequential access.

In practice, Selection sort performs better on Array compared to Linked List as:

• In array, data is stored sequentially in memory while in case of Linked List, it is scattered.
• When a memory access is made, adjacency memory is placed in the cache and hence, the second memory access of an adjancent element is instant
• Due to this, Array is Cache friendly and results in better performance despite having the same Time Complexity.

## Summarization:

Worst Case Complexity Average Case Complexity Best Case Complexity Space Complexity
O(n^2) O(n^2) O(n^2) O(1)

Thank You!
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://iq.opengenus.org/time-and-space-complexity-of-selection-sort-on-linked-list/", "fetch_time": 1680211032000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2023-14/segments/1679296949387.98/warc/CC-MAIN-20230330194843-20230330224843-00086.warc.gz", "warc_record_offset": 378676226, "warc_record_length": 21654, "token_count": 1318, "char_count": 4732, "score": 3.546875, "int_score": 4, "crawl": "CC-MAIN-2023-14", "snapshot_type": "longest", "language": "en", "language_score": 0.5827127695083618, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00004-of-00064.parquet"}