0% found this document useful (0 votes)
20 views2 pages

Quick Sort Cod

The document describes algorithms for quick sort and merge sort. It includes code snippets in C++ for implementing both algorithms. The quick sort code includes functions for sorting an array and swapping elements. The merge sort code includes functions for sorting, merging sorted halves, and displaying the array.

Uploaded by

Mihai
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)
20 views2 pages

Quick Sort Cod

The document describes algorithms for quick sort and merge sort. It includes code snippets in C++ for implementing both algorithms. The quick sort code includes functions for sorting an array and swapping elements. The merge sort code includes functions for sorting, merging sorted halves, and displaying the array.

Uploaded by

Mihai
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/ 2

QUICK SORT COD (CU MESAJE) i++;

#include <iostream> j--;

using namespace std; }

int n, v[100001]; ///cum e vectorul??

afisare();

void afisare() }

{ if(st<j)

for(int k=1;k<=n;k++) QuickSort(st,j);

cout<<v[k]<<' '; if(i<dr)

cout<<endl; QuickSort(i,dr);

} }

void QuickSort(int st, int dr) int main()

{ {

int i,j,aux,mij; int i;

i=st; cin>>n;

j=dr; for(i=1; i<=n; i++)

mij=v[(st+dr)/2]; cin>>v[i];

cout<<"-------------------------------------------\n"; QuickSort(1,n);

cout<<"intervalul indici:"<<st<<' '<<dr<<" for(i=1; i<=n; i++)


pivot:"<<mij<<endl;
cout<<v[i]<<" ";
while(i<=j)
return 0;
{
}
while(v[i]<mij) i++;
MERGE SORT (CU MESAJE)
while(mij<v[j]) j--;
#include <iostream>
if(i<=j)
using namespace std;
{
int n, v[100001];
cout<<"fac schimbul intre
"<<v[i]<<"_"<<v[j]<<endl; void afisare(int p1, int p2)

aux=v[i]; {

v[i]=v[j]; for(int k=p1; k<=p2; k++)

v[j]=aux; cout<<v[k]<<" ";


cout<<endl; afisare(1,n);

} }

void Interclasare(int v[],int st, int mij, int dr) void MergeSort( int v[], int st, int dr)

{ cout<<"capetele sunt "<<st<<" "<<dr<<endl; {

cout<<"prima jumatate"<<endl; if(st==dr)

afisare(st,mij); v[st]=v[dr];

cout<<"a doua jumatate"<<endl; else if(st+1==dr)

afisare(mij+1,dr); {

int i=st; if(v[st]>v[dr])

int j=mij+1; swap(v[st],v[dr]);

int a[101]; }

int k=0; else

while(i<=mij && j<=dr) {

{ int mij=(st+dr)/2;

if(v[i]<v[j]) a[++k]=v[i++]; MergeSort(v,st,mij);

else MergeSort(v,mij+1,dr);

if(v[i]>v[j]) a[++k]=v[j++]; Interclasare(v,st,mij,dr);

else }

{ }

a[++k]=v[i++]; int main()

a[++k]=v[j++]; {

} int i;

} cin>>n;

while(i<=mij) for(i=1; i<=n; i++)

a[++k]=v[i++]; cin>>v[i];

while(j<=dr) MergeSort(v,1,n);

a[++k]=v[j++]; for(i=1; i<=n; i++)

for(i=st,k=1; i<=dr; i++,k++) cout<<v[i]<<" ";

v[i]=a[k]; return 0;

cout<<"dupa interclasare"<<endl; }

You might also like