how can calculate time complexity of selection sort step by step?

enter image description here
I should solve the time Complexity method like this photo but I have a problem solving it for selectio sort.
I solved it in general and not in detail like I wanted going to know the solution method and not just like (T(n)= O(n-1)+..+1. I want it to be step by step how we reached this answer and how we produced the time (O(n^2)).
This is the code that I made through which I want to know the time complexity:
package example;

public class Main1 {

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public static void main(String[] args) {
int arr[] = { 71, 2, 15, 43, 1, 30, 8 };
selectionSort(arr);
System.out.print("[ ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ,");
}
System.out.print(" ]");
}
static void selectionSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
int min = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[min] > arr[j]) {
min = j;
}
}
int temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
}
</code>
<code>public static void main(String[] args) { int arr[] = { 71, 2, 15, 43, 1, 30, 8 }; selectionSort(arr); System.out.print("[ "); for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " ,"); } System.out.print(" ]"); } static void selectionSort(int[] arr) { for (int i = 0; i < arr.length - 1; i++) { int min = i; for (int j = i + 1; j < arr.length; j++) { if (arr[min] > arr[j]) { min = j; } } int temp = arr[i]; arr[i] = arr[min]; arr[min] = temp; } } </code>
public static void main(String[] args) {

    int arr[] = { 71, 2, 15, 43, 1, 30, 8 };
    selectionSort(arr);
    System.out.print("[ ");
    for (int i = 0; i < arr.length; i++) {
        System.out.print(arr[i] + " ,");
    }
    System.out.print(" ]");
}

static void selectionSort(int[] arr) {
    for (int i = 0; i < arr.length - 1; i++) {
        int min = i;
        for (int j = i + 1; j < arr.length; j++) {
            if (arr[min] > arr[j]) {
                min = j;
            }
        }
        int temp = arr[i];
        arr[i] = arr[min];
        arr[min] = temp;
    }

}

}

For selection sort the task is much easier than for merge sort, for the following reasons:

  • Contrary to merge sort, selection sort does not apply recursion, so you don’t actually work with a recurrence relation 𝑇(𝑛).

  • Contrary to merge sort, the number of iterations made by selection sort does not depend on the result of data comparisons, but only on the size of the array. So with selection sort there is no concept of worst case, best case: all cases are equal.

Analysis

The two (nested) loops in selection sort are really visiting all possible pairs of (𝑖, 𝑗), where 0 ≤ 𝑖 < 𝑗 < 𝑛. So the inner if statement is executed as many times as there are such pairs (𝑖, 𝑗).

This number is equal to “𝑛-choose-2”, i.e. a binomial coefficient, and it is equal to 𝑛(𝑛−1)/2.

Whether or not the body of the if statement is executed is not relevant for time complexity analysis, since that assignment to min has a time complexity of O(1), just like the evaluation of the if condition. So for one execution of the whole if statement, we either have O(1) or O(1+1), which still is O(1), either way.

The three swap instructions also have a O(1) time complexity and are executed 𝑛−1 times.

There is also the overhead of evaluating the outer for initialisation and exit.

Adding all that together we have this time complexity:

θ(1) + θ(𝑛−1) + θ(𝑛(𝑛−1)/2) = θ(𝑛²).

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật