Quick sorting, insert sorting, bubble sorting, sorting

Quick sort: O(n2)

True correct Quick sort:

#include

#include

using namespace std;
int a[100];
/*void swap(int a,int b){
int tmp;
tmp=a;
a=b;
b=tmp;
}
*/
void quicksort(int left,int right){
int i,j,t;
if(left>right)
return;
int temp=a[left];
i
=left;
j
=right;
while(i<j){
while(a[j]>temp&&i<j)
j
--;
a[i]
=a[j];
while(a[i]j)
i++;
a[j]
=a[i];
}
a[i]
=temp;
quicksort(left,i
-1);
quicksort(i
+1,right);
}
int main(){
int num;
cin
>>num;
for(int i=1;i<=num;i++)
scanf_s(
"%d",&a[i]);
quicksort(
1,num);
for(int i=1;i<=num;i++)
cout
<endl;
return 0;
}

Insert sort: O(n2)

 #include

#include
using namespace std;
int a[100];
int main(){
int n;
cin>>n;
for(int i=0;i cin>>a[i];
for(int i=1;i if(a[i] int j=i-1;
int x=a[i];
while(j>=0&&a[j]>x){
a[j+1]=a[j];//It is found that the former number is greater than the latter number
Move the following number back.
j--;
}
a[j+1]=x;//Insert the number to be compared
}
}
for(int i=0;i cout< return 0;
}

Merge and sort: O(nlogn)

#include 

#include

using namespace std;
void merge(vector<int> &arr,< span style="color: #0000ff;">int L,int mid,int R){
int *help = new int(R-L+1);
int p1=L,p2=mid+1, i=0;
while(p1<=mid && p2<=R) {
help[i
++] = arr[p1]>arr[p2]? arr[p2++]: arr[p1++];
}
while(p1<=mid)
help[i
++] = arr[p1++];
while(p2<=R)
help[i
++] = arr[p2++];
for (int i=0;i1;i++)
{
arr[L
+i] = help[i];
}
}
void sortprocess(vector<int> &arr,< span style="color: #0000ff;">int L,int R)
{
if (L < R) {
int mid = L + ((RL)>>2); // (L+R)/2
sortprocess(arr,L,mid);
sortprocess(arr,mid
+1,R);
merge(arr,L,mid,R); }}
void MergeSort(vector<int> &arr,< span style="color: #0000ff;">int
L,int R){
if (arr.size()<2< span style="color: #000000;">)
return; sortprocess(arr,L,R);}
int main(){
vector
<int> arr;
int n,temp; cin>>n; //Enter n number
for (int i=0;i)
{
cin
>>temp; //Enter data
arr.push_back(temp);
}
MergeSort(arr,
0,arr.size()-1< /span>);
for(int i=0;i)
cout
<endl;
system("pause");
return 0;
}

Really correct quick sort:

#include

#include

using namespace std;
int a[100];
/*void swap(int a,int b){
int tmp;
tmp=a;
a=b;
b=tmp;
}
*/
void quicksort(int left,int right){
int i,j,t;
if(left>right)
return;
int temp=a[left];
i
=left;
j
=right;
while(i<j){
while(a[j]>temp&&i<j)
j
--;
a[i]
=a[j];
while(a[i]j)
i++;
a[j]
=a[i];
}
a[i]
=temp;
quicksort(left,i
-1);
quicksort(i
+1,right);
}
int main(){
int num;
cin
>>num;
for(int i=1;i<=num;i++)
scanf_s(
"%d",&a[i]);
quicksort(
1,num);
for(int i=1;i<=num;i++)
cout
<endl;
return 0;
}

#include

#include
using namespace std;
int a[100];
int main(){
int n;
cin>>n;
for(int i=0;i cin>>a[i];
for(int i=1;i if(a[i] int j=i-1;
int x=a[i];
while(j>=0&&a[j]>x){
a[j+1]=a[j];//It is found that the former number is greater than the latter number
Move the following number back.
j--;
}
a[j+1]=x;//Insert the number to be compared
}
}
for(int i=0;i cout< return 0;
}

#include 

#include

using namespace std;
void merge(vector<int> &arr,< span style="color: #0000ff;">int L,int mid,int R){
int *help = new int(R-L+1);
int p1=L,p2=mid+1, i=0;
while(p1<=mid && p2<=R) {
help[i
++] = arr[p1]>arr[p2]? arr[p2++]: arr[p1++];
}
while(p1<=mid)
help[i
++] = arr[p1++];
while(p2<=R)
help[i
++] = arr[p2++];
for (int i=0;i1;i++)
{
arr[L
+i] = help[i];
}
}
void sortprocess(vector<int> &arr,< span style="color: #0000ff;">int L,int R)
{
if (L < R) {
int mid = L + ((RL)>>2); // (L+R)/2
sortprocess(arr,L,mid);
sortprocess(arr,mid
+1,R);
merge(arr,L,mid,R); }}
void MergeSort(vector<int> &arr,< span style="color: #0000ff;">int
L,int R){
if (arr.size()<2< span style="color: #000000;">)
return; sortprocess(arr,L,R);}
int main(){
vector
<int> arr;
int n,temp; cin>>n; //Enter n number
for (int i=0;i)
{
cin
>>temp; //Enter data
arr.push_back(temp);
}
MergeSort(arr,
0,arr.size()-1< /span>);
for(int i=0;i)
cout
<endl;
system("pause");
return 0;
}

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 5280 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.