Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
130 views

Elementry Sorting Algorithm - LeetCode Discuss

The document discusses and provides code implementations for four elementary sorting algorithms: bubble sort, selection sort, insertion sort, and quick sort. It states that the time complexity of bubble sort, selection sort, and insertion sort is O(N^2) while the space complexity is O(1). Quick sort is not analyzed. Code snippets are provided to implement each of the sorting algorithms.

Uploaded by

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

Elementry Sorting Algorithm - LeetCode Discuss

The document discusses and provides code implementations for four elementary sorting algorithms: bubble sort, selection sort, insertion sort, and quick sort. It states that the time complexity of bubble sort, selection sort, and insertion sort is O(N^2) while the space complexity is O(1). Quick sort is not analyzed. Code snippets are provided to implement each of the sorting algorithms.

Uploaded by

A02Arnab Biswas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

3/23/23, 10:56 PM (10) Elementry Sorting Algorithm - LeetCode Discuss

New 10
Explore Problems Interview Contest Discuss Store Premium 387

Back Elementry Sorting Algorithm

R_aghav 73 Last Edit: March 15, 2023 3:46 PM 54 VIEWS S


h
a
2 Time complexity of first three Algorithm are same that is O(N^2) and Space Complexity will be O(1) r
e

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

//Bubble Sort
void bubble(int arr[],int n){
//TC=O(n^2) , SC=O(1)
for(int i=n-1;i>=0;i--){
for(int j=0;j<=i-1;j++){
if(arr[j]>arr[j+1]){
swap(arr[j],arr[j+1]);
}
}
}
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}
cout<<endl;
}

//Selection Sort
void selection(int arr[],int n){
//TC=O(n^2) , SC=O(1)
for(int i=0;i<n;i++){
int mini=i;
for(int j=i+1;j<n;j++){
if(arr[i]<arr[mini]){
mini=i;
}
}
swap(arr[i],arr[mini]);
}
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}
cout<<endl;
}

//Insertion Sort
void insertion(int arr[],int n){
//TC=O(n^2) , SC=O(1)
for(int i=0;i<n;i++){
int j=i;
while(j>0 && arr[j-1]>arr[j]){
swap(arr[j-1],arr[j]);
j--;
}
}
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}
cout<<endl;
}

//Quick Sort
int partiton(int arr[],int l,int r){
int pivo=arr[r];
i t i l 1
https://leetcode.com/discuss/study-guide/3237473/elementry-sorting-algorithm 1/1

You might also like