1 / 9

VECTORI Metode de sortare

VECTORI Metode de sortare. Proiect realizat de Vicentiu Mihaela Raluca Clasa a X-a B Colegiul National “ Mihai Eminescu ”. Metoda Bulelor  (bubblesort ). Descriere :. Program C++ :. #include <iostream.h> using namespace std ; int main() { int v[100 ], i, n, ok ,aux ;

louie
Download Presentation

VECTORI Metode de sortare

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. VECTORIMetode de sortare Proiect realizat de Vicentiu Mihaela Raluca Clasa a X-a B Colegiul National “MihaiEminescu”

  2. MetodaBulelor (bubblesort) Descriere : Program C++ : #include <iostream.h> using namespace std; int main() { int v[100],i,n, ok,aux; cout<<"n= ";cin>>n; for(i=1;i<=n;i++) {    cout<<"v["<<i<<"]=“;cin>>v[i]; } do { ok=1; for(i=1;i<=n;i++)  if(v[i]>v[i+1])   {aux=v[i]; v[i]=v[i+1]; v[i+1]=aux;     } } while(ok==0); cout<<"Vectorul sortat este: "; for(i=1;i<=n;i++) cout<<v[i]<<" "; return 0; } • Parcurgevectorul de maimulteoripanacandilsorteaza. • Comparafiecare element cu succesorulsausi se interchimbaintreeledaca nu sunt in ordine. • Vectorulestesortatcand la o parcurgerecompleta nu se efectueazaniciointerchimbare .

  3. Metodaprininsertie Descriere : Program C++ : • Aceasta metoda aseaza elementul citit pe pozitia lui finala in vector, comparandu-l cu valorile introduse deja si interschimbandul pana ajunge in pozitia corecta. • #include <iostream.h> using namespace std; int main() {int v[100],n,i,j,aux; cout<<"n= ";cin>>n; cout<<"v[1]= ";cin>>v[1]; for(i=2;i<=n;i++) { cout<<"v["<<i<<"]= "; cin>>v[i]; j=i; while(v[j]<v[j-1] && j>1) { aux=v[j]; v[j]=v[j-1]; v[j-1]=aux; j--; } } cout<<"Vectorul sortat este: "; for(i=1;i<=n;i++) cout<<v[i]<<" "; return 0; }

  4. Metodadirecta Descriere : Program C++ : • Parcurgevectorul o singura data. • Se verificacomparandelementul de pepozitiacurenta cu toateelementele de dupaaceastapozitie. • Undeelementele nu sunt in ordine se interchimba. • #include <iostream> using namespace std; int main () { int v[50],n,i,j,aux; cin>>n; for(i=0;i<=n;i++) for(j=i+1;j<=n;j++) if(v[i]>v[j] && i%2==0 && j%2==0) { aux=v[i]; v[i]=v[j]; v[j]=aux; } cout<< “\n vectorulsortat:”; for(i=0;i<=n;i++) cout<<v[i]<<“ “; return 0; }

  5. Descriere : Program C++ : • Aflamminimulsiilducempeultimapozitie a vectoruluiprincompararesiinterchimbarein caz de dezordine. • Procedeul se repetapentrufiecare element . • #include <iostream.h> using namespace std; int main() { int v[100],i,n,min,k,aux,j; cout<<"n= ";cin>>n; for(i=1;i<=n;i++) { cout<<"v["<<i<<"]= “; cin>>v[i[; } for(i=1;i<=n-1;i++) { min=v[i]; k=i; for(j=i+1;j<=n;j++) if(v[j]<min) { min=v[j]; k=j; } aux=v[i]; v[i]=v[k]; v[k]=aux;} cout<<"Vectorul sortat este: "; for(i=1;i<=n;i++) cout<<v[i]<<" "; return 0; } Metodade sortareprin minim

  6. Metodainserarii Descriere : Programul C++ : Se folosesc 2 vectori: vectorulsursa: a (nesortat) sivectoruldestinatie : b (sortat). Elementelevectoruluisursa se copiaza in vectoruldestinatieprininserare in pozitiacorespunzatoare ,astfelincat in vectoruldestinatiesa fie respectararelatia de ordine. #include <iostream> using namespace std; int main () {intI,j,n,k,a[50],b[50]; cout<<“n=“; cin>>n; for(i=0;i<=n;i++) {cout<<“a[“<<i<<“]= “; cin>>a[i];} b[0]=a[0]; for(i=1;i<n;i++) {j=0; while (j<=i-1&& a[i]>b[j]) j++; for( k=I;k>j+1;k--) b[k]=b[k-1]; b[j]=a[i]; } for(i=0;i<=n;i++) cout<<b[i]<<“ “; return 0; }

  7. Descriere : Program C++ : #include <iostream> using namespace std; int main () {inti,j,n,aux,a[50]; cout<<“n=“; cin>>n; for(i=0;i<n;i++) { cout<<“a[“<<i+1<<“]= “; cin>>a[i];} for(i=0;i<n;i++) {aux=a[i]; j=i-1; while (j>0 && aux<a[j]) {a[j+1]=a[j]; j--;} if(aux>a[j]) a[j+1]=aux; else {a[1]=a[0]; a[0]=aux;} } for(i=0;i<=n;i++) cout<<a[i]<< “ “ ; return 0; } • Vectorul se imparte in 2 subvectori : subvectorulsursa: a[i] sisubvectoruldestinatie: a[0]. • Elementul a[i] sin subvectorulsursaesteinserat in subvectoruldestinatie conform relatiei de ordine , astfelvectoruldestinatieva fi mereu un vector ordonat . Metodainserariidirecte

  8. Metodainserariirapide Descriere : Program C++ : #include <iostream> using namespace std; int main () {inti,j,n,aux,st,dr,mijl,a[50]; cout<<“n=“; cin>>n; for(i=0;i<n;i++) { cout<<“a[“<<i+1<<“]= “;cin>>a[i];} for(i=0;i<n;i++) {aux=a[i]; st=0; dr=i-1; while (st<=dr) {mijl=(st+dr)/2; if (aux<a[mijl]) dr=mijl-1; else st=mijl+1;} j=i-1; while (j>=st) {a[j+1]=a[j]; j=j-1;} a[st]=aux; } for(i=0;i<‘;i++) cout<<a[i]<< “ “ ; return 0; } • Vectorul se imparte in doisubvectori : subvectorulsursasisubvectoruldestinatie(ordonat). • Pozitiaelementului a[i] va fi gasita cu prinalgoritmul de cautarebinara. • Subvectoruldestinatieesteimpartit in doisubvectori se examineazarelatie de ordinedintremijlocsivectorul a[i] si se stabilestedacavectorul se insereaza in prima sau a douajumatate . Operatia de divizare a subvectorului continua pana se gasestepozitia in care urmeazasa fie inseray a[i].

  9. Thanks for watching !!!

More Related