0% found this document useful (0 votes)
23 views

Sortarea Prin Selecţie Directă

The document contains code examples for sorting arrays using the bubble sort and selection sort methods, inserting elements into a sorted array, using enumerations to define constants, defining and using structures, and dynamically allocating memory.

Uploaded by

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

Sortarea Prin Selecţie Directă

The document contains code examples for sorting arrays using the bubble sort and selection sort methods, inserting elements into a sorted array, using enumerations to define constants, defining and using structures, and dynamically allocating memory.

Uploaded by

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

SORTAREA PRIN METODA BULELOR

#include<iostream>
#include<conio.h>
using namespace std;
int a[200000],aux,f,n,i;
main() {
cout<<"n=";cin>>n;
for(i=0;i<n;i++){
cout<<"a["<<i+1<<"]="; cin>>a[i];
}
do{
f=0;
for(i=0;i<n-1;i++)
if(a[i]>a[i+1]){
aux=a[i];
a[i]=a[i+1];
a[i+1]=aux;f=1;
}
}while(!f);
cout<<"sirul sortat este"<<endl;
for(i=0;i<n;i++)cout<<a[i]<<" ";
getch();
}
SORTAREA PRIN SELECŢIE DIRECTĂ
#include<iostream>
#include<conio.h>
using namespace std;
int a[200000],aux,f,n,i,j;
main() {
cout<<"n=";cin>>n;
for(i=0;i<n;i++){
cout<<"a["<<i+1<<"]=";
cin>>a[i];
}
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(a[j]<a[i]) {
aux=a[i];
a[i]=a[j];
a[j]=aux;
}
cout<<"sirul sortat este"<<endl;
for(i=0;i<n;i++)cout<<a[i]<<" ";
getch();
}
SORTAREA PRIN INSERTIE
#include <iostream>
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;
}

EXEMPLU DE ENUMERARE

#include <iostream>
using namespace std;
enum poli {Est=1, Vest=2, Sud=3, Nord=4};
int directie;
main(){
cout<<"Introdu directia:"<<endl;
cout<<"Est=1, Vest=2, Sud=3, Nord=4 "<<endl;
cin>>directie;
switch(directie){
case Est:cout<<"Vreau sa merg spre Est";break;
case Vest:cout<<"reau sa merg spre Est";break;
case Sud:cout<<"Vreau sa merg spre Est";break;
case Nord:cout<<"Vreau sa merg spre Est";break;
default: cout<<"Merg unde vreau eu!";break;
}
}
EXEMPLU DE STRUCTURI

#include <iostream>
using namespace std;
struct Elev
{ char nume [20];
char prenume [20];
float media;
};

void citire_elev(Elev &e)


{
cout<<"Nume: " ; cin>>e.nume;
cout<<"Prenume: " ; cin>>e.prenume;
cout<<"Media: " ; cin>>e.media;
}

void afisare_elev(Elev e)
{
cout<<"Nume: " ; cout<<e.nume<<endl;
cout<<"Prenume: " ; cout<<e.prenume<<endl;
cout<<"Media: " ; cout<<e.media<<endl;
}

int main()
{
Elev e;
citire_elev(e);
afisare_elev(e);
return 0;
}
EXEMPLU DE ALOCAREA DINAMICA
#include<iostream>
#include<iomanip>
using namespace std;
int *t, Max, poz, i, n;
main() {
cout<<"Dati numarul de elemente n="; cin>>n;
t=new int[n];
for (i=0; i<n; i++) {cout<<"t["<<i<<"]="; cin>>t[i]; }
cout<<"Ati introdus numerele "<<endl;
for (i=0; i<n; i++) cout<<setw(4)<<t[i];
Max=t[0];
for ( i=1; i<n; i++) if ( Max<t[i] ) {Max=t[i]; poz=i; }
cout<<"Elementul maxim "<<Max<<endl;
cout<<"Pozitia elementului maxim este "<<poz;
delete []t;
}

You might also like