Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Bouble sort
- #include <iostream>
- using namespace std;
- int main() {
- int n;
- cout << "Cate elemente are vectorul?"; cin >> n;
- int v[20];
- cout << "Introdu elementele vectorului:" << endl;
- for (int i = 0; i < n; i++)
- cin >> v[i];
- int ok, i = 0;
- do {
- ok = 1;
- for (int j = 0; j < n - i - 1; j++) {
- if (v[j] > v[j + 1]) {
- swap(v[j], v[j + 1]);
- ok = 0;
- }
- }
- i++;
- } while (!ok);
- for (int i = 0; i < n; i++)
- cout << v[i] << " ";
- return 0;
- }
- ----------------------------------------------------------
- //Insertion Sort
- #include <iostream>
- using namespace std;
- int main() {
- int n;
- cout << "Cate elemente are vectorul?"; cin >> n;
- int v[20];
- cout << "Introdu elementele vectorului:" << endl;
- for (int i = 0; i < n; i++)
- cin >> v[i];
- for (int i = 1; i < n; i++) {
- int ind = i - 1;
- int arr = v[i];
- while (ind >= 0 && arr < v[ind]) {
- v[ind + 1] = v[ind];
- ind--;
- }
- v[ind + 1] = arr;
- }
- for (int i = 0; i < n; i++)
- cout << v[i] << " ";
- return 0;
- }
- ----------------------------------------------------------
- //Selction sort
- #include <iostream>
- using namespace std;
- int main() {
- int n;
- cout << "Cate elemente are vectorul?"; cin >> n;
- int v[20];
- cout << "Introdu elementele vectorului:" << endl;
- for (int i = 0; i < n; i++)
- cin >> v[i];
- for (int m = n - 1; m >= 0; m--) {
- int nmax = v[0];
- int ind = 0;
- for (int i = 0; i <= m; i++) {
- if (v[i] > nmax) {
- nmax = v[i];
- ind = i;
- }
- }
- swap(v[m], v[ind]);
- }
- for (int i = 0; i < n; i++)
- cout << v[i] << " ";
- return 0;
- }
- ----------------------------------------------------------
- //Counting sort
- #include <iostream>
- using namespace std;
- int main() {
- int n;
- cout << "Cate elemente are vectorul?"; cin >> n;
- int v[20];
- cout << "Introdu elementele vectorului:" << endl;
- for (int i = 0; i < n; i++)
- cin >> v[i];
- int c[10], b[10];
- for (int i = 0; i < n; i++)
- b[i] = 0;
- for (int i = 0; i < n - 1; i++) {
- for (int j = i + 1; j < n; j++) {
- if (v[i] > v[j])
- b[i]++;
- else
- b[j]++;
- }
- }
- for (int i = 0; i < n; i++)
- c[b[i]] = v[i];
- for (int i = 0; i < n; i++)
- cout << c[i] << " ";
- return 0;
- }
- ----------------------------------------------------------
- //Interclasare
- #include <iostream>
- using namespace std;
- int main() {
- int n;
- cout << "Cate elemente are vectorul?"; cin >> n;
- int v[20];
- cout << "Introdu elementele vectorului:" << endl;
- for (int i = 0; i < n; i++)
- cin >> v[i];
- int x, y, a[10], b[10], c[20], k = 0;
- cout << "Cate elemente are primul vector??"; cin >> x;
- cout << "Introduceti elemntele primului vector:" << endl;
- for (int i = 0; i < x; i++) cin >> a[i];
- cout << "Cate elemente are cel de-al doilea vector??"; cin >> y;
- cout << "Introdu elemntele celui de-al doilea vector:" << endl;
- for (int i = 0; i < y; i++) cin >> b[i];
- int i = 0, j = 0;
- while (i < x && j < y) {
- if (a[i] > b[j])
- c[k++] = b[j++];
- else
- c[k++] = a[i++];
- }
- for (int d = i; d < x; d++)
- c[k++] = a[d];
- for (int e = j; e < y; e++)
- c[k++] = b[e];
- for (i = 0; i < x + y; i++)
- cout << c[i] << " ";
- return 0;
- }
- ----------------------------------------------------------
- //Cautare binara
- #include <iostream>
- using namespace std;
- int main() {
- int n,a[100],st,dr,m,i,gasit = 0, x;
- cout<<"n=";
- cin>>n;
- cout<<"Elementele vectorului sunt";
- for(i = 0; i < n ; i++)
- cin>>a[i];
- cout<<"Introduceti elementul cautat";
- cin>>x;
- st = 0;
- dr = n-1;
- while(st <= dr && !gasit)
- {
- m = (st + dr) / 2;
- if(a[m] == x)
- gasit = 1;
- else if(x < a[m])
- dr = m - 1;
- else
- st = m + 1:
- }
- if(gasit)
- cout<<"Elementul se afla in sir";
- else
- cout<<"Elementul nu se afla in sir";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement