Jump to content
cmiN

[C++] Algoritmi de sortare II [cmiN]

Recommended Posts

Raspuns la http://rstcenter.com/forum/29838-python-algoritmi-de-sortare-console-cmin.rst

Desi sunt tehnici diferite, algoritmii sunt similari cu cei de mai sus, numai ca sunt ceva mai eficienti.

Ca performanta average:

insertionsort > bubblesort (O(n^2/2))

mergesort > quicksort (O(nlogn))

Mergesort are worst O(nlogn) pe cand quicksort are O(n^2).

#include <iostream>
#define nmax 128

using namespace std;

void insertionsort(int vec[], int vlen)
{
// avg and wst: O(pow(n, 2)/2)
int tmp, i, j;
for (i = 1; i < vlen; i++) {
tmp = vec[i];
for (j = i - 1; j >= 0 && tmp < vec[j]; j--) {
vec[j + 1] = vec[j];
}
vec[j + 1] = tmp;
}
}

void mergesort(int vec[], int vlen)
{
// avg and wst: O(n*log(n, 2))
if (vlen > 1) {
int mlen, i, j, k;
int* tvec = new int[vlen];
for (i = 0; i < vlen; i++) {
tvec[i] = vec[i];
}
mlen = vlen / 2;
mergesort(tvec, mlen);
mergesort(tvec + mlen, vlen - mlen);
if (tvec[mlen - 1] < tvec[mlen]) {
for (i = 0; i < vlen; i++) {
vec[i] = tvec[i];
}
} else {
for (i = 0, j = mlen, k = 0; i < mlen && j < vlen; k++) {
if (tvec[i] < tvec[j]) {
vec[k] = tvec[i];
i++;
} else {
vec[k] = tvec[j];
j++;
}
}
while (i < mlen) {
vec[k++] = tvec[i++];
}
while (j < vlen) {
vec[k++] = tvec[j++];
}
}
delete[] tvec;
}
}

int main()
{
int vec[nmax], vlen, i;
cout << "Vector length: ";
cin >> vlen;
for (i = 0; i < vlen; i++) {
cout << "vec[" << i << "]: ";
cin >> vec[i];
}
// sterge slash-urile corespunzatoare functiei pe care vrei s-o folosesti
//insertionsort(vec, vlen);
//mergesort(vec, vlen);
for (i = 0; i < vlen; i++) {
cout << vec[i] << " ";
}
return 0;
}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...