0% found this document useful (0 votes)
40 views3 pages

Algoritmi Esentiali Vectori

The document describes 4 algorithms for vectors: 1. Sorting a vector in ascending or descending order using a bubble sort algorithm. 2. Inserting the average of a vector's elements in the middle of the vector. 3. Deleting positive elements from a vector by shifting the remaining elements. 4. Interleaving the elements of two vectors into a new sorted vector.
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)
40 views3 pages

Algoritmi Esentiali Vectori

The document describes 4 algorithms for vectors: 1. Sorting a vector in ascending or descending order using a bubble sort algorithm. 2. Inserting the average of a vector's elements in the middle of the vector. 3. Deleting positive elements from a vector by shifting the remaining elements. 4. Interleaving the elements of two vectors into a new sorted vector.
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/ 3

Algoritmi esentiali vectori

 Ordonare crescator/descrescator
#include <iostream>
using namespace std;
int main()
{
int i, j, n, a[100], aux;
cin >> n;

for (i = 0; i < n; i++)


cin >> a[i];

for (i = 0; i < n - 1; i++)


for (j = i+1; j < n; j++)
if(a[i] > a[j]) { // if (a[i] < a[j])
aux = a[i];
a[i] = a[j];
a[j] = aux; }

for (i = 0; i < n; i++)


cout << a[i] << " ";

return 0;
}

 Inserare in vector
#include <iostream>
using namespace std;
int main()
{
float a[100], s = 0;
int n, i, j;
cin >> n;

for (i = 0; i < n; i++)


cin >> a[i];

for (i = 0; i < n; i++)


s += a[i]; // suma elementelor vectorului
for (j = n; j >= n/2; j--)
a[j+1] = a[j]; // am mutat elementele inainte de dupa mijloc cu o casuta

a[n/2] = s/n; // insereaza media aritmetica la mijlocul vectorului


n++;

for (i = 0; i < n; i++)


cout << a[i] << " ";

return 0;
}

 Stergere din vector


#include <iostream>
using namespace std;
int main () {
int a[100], n, i, j;
cin >> n;

for (i = 0; i < n; i++)


cin >> a[i];

for (i = 0; i < n; i++)


if (a[i] > 0) { // sterge elementele pozitive
for (j = i; j < n-1; j++)
a[j] = a[j+1];
n--;
i--;
}

for (i = 0; i < n; i++)


cout << a[i] << " ";

return 0;

}
 Interclasare
#include <iostream>
using namespace std;
int main()
{
int a[100], b[100], c[200];
int n, m, k = 0, i = 0, j = 0;;
cin >> n >> m;
for(int i = 0; i < n; i++)
cin >> a[i];
for(int i = 0; i < m; i++)
cin >> b[i];

while(i < n && j < m)


if(a[i] < b[j])
c[k++] = a[i++];
else
c[k++] = b[j++];

if(i <= n)
for(int p = i; p < n; p++)
c[k++] = a[p];

if(j <= m)
for(int p = j; p < m; p++)
c[k++] = b[p];

for(int p = 0; p < k; p++)


cout << c[p] << " ";

return 0;
}

You might also like