0% found this document useful (0 votes)
66 views4 pages

For (J 1 J N J++) (

The document describes several sorting algorithms: 1. Insertion sort works by iterating through an array and inserting each element into its sorted position. 2. Selection sort iterates through an array and finds the minimum element, swapping it into the front. 3. Bubble sort iterates through pairs of adjacent elements, swapping them if out of order until the array is fully sorted. 4. Other algorithms described include shell sort, merge sort, quicksort, counting sort, and heapsort.

Uploaded by

davidvpe
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views4 pages

For (J 1 J N J++) (

The document describes several sorting algorithms: 1. Insertion sort works by iterating through an array and inserting each element into its sorted position. 2. Selection sort iterates through an array and finds the minimum element, swapping it into the front. 3. Bubble sort iterates through pairs of adjacent elements, swapping them if out of order until the array is fully sorted. 4. Other algorithms described include shell sort, merge sort, quicksort, counting sort, and heapsort.

Uploaded by

davidvpe
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Insertion

for (j = 1; j<n; j++ ) {


Key = A[j];
i = j-1;
while (i > -1 && A[i] > Key ){
A[i+1]=A[i];
i=i-1;
}
A[i+1] = Key;
}

Selección

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


small=i;
for(j=i+1; j<n; j++)
if(A[j]<A[small])
small=j;
temp=A[small]; // intercambio
A[small]=A[i];
A[i]=temp;
}

Bubble

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


{ intercambio = false;
for (j=1; j<n-i; j++)
{ if (v[j]<v[j-1])
{ aux=v[j];
v[j]=v[j-1];
v[j-1]=aux;
intercambio = true
}
}
if (!intercambio)
break;
}

Shell

public void ShellSort(int [] list)


{
int j,inc;
inc=list.length/2;
while (inc>0) {
for(int i=inc+1;i<list.length;i++) {
j=i-inc;
while (j>0) {
if (list[j] > list[j+inc]) {
Swap(list[j],list[j+inc]);
j=j-inc;
} else {
j=0;
}
}
}
inc=inc/2;
}
}

MergeSort

void mergeSort (int a[], int n) {


mSort(a, 0, a.length-1);
}
void mSort (int a[], int l, int r) {
if ( l>=r ) return;
int m = (l+r)/2;
mSort (a, l, m);
mSort (a, m+1, r);
merge (a, l, m, r);
}

void merge(vector<T>& v, int ini, int med, int fin) {


vector<T> aux(fin - ini + 1);
int i = ini; // Índice de la parte izquierda
int j = med + 1; // Índice de la parte derecha
int k = 0; // Índice del vector aux
while (i <= med && j <= fin) { // Combinamos mientras existan elementos en los 2
arr.
if (v[i] < v[j]) {
aux[k] = v[i]; i++;
}
else {
aux[k] = v[j]; j++;
}
k++;
}
while (i <= med) { // Copiamos los elementos que faltaban
aux[k] = v[i]; i++; k++;
}
while (j <= fin) { // Copiamos los elementos que faltaban
aux[k] = v[j]; j++; k++;
}
/* Copiamos los elementos ordenados de aux al vector original v */
for (int n = 0; n < aux.size(); ++n) v[ini + n] = aux[n];
}
QuickSort

void quickSort (int arr[], int left, int right) {


if (left>=right) return;
int mid = partition(arr, left, right);
quickSort(arr,left,mid-1);
quickSort(arr,mid+1,right);
}

int partition (int arr[], int left, int right) {


int i=left+1; // left index
int j=right; // right index
int pivot=arr[left]; // pivot
while (i<=j) {
if (arr[i]<=pivot) i++;
else if (arr[j]>pivot) j--;
else swap(arr,i,j); }
swap(arr,left,j);
return j;
}

CountingSort

for (int i=0;i<k+1;i++)


C[i]=0;
for (int j=0;j<n;j++)
C[A[j]] = C[A[j]] + 1;
// C[i] contiene la cantidad de elementos en i
for (int i=1;i<k+1;i++)
C[i] = C[i]+C[i-1];
// C[i] contiene ahora la cantidad de elementos menores o iguales a i
for (int j=n-1;j>-1;j--)
B[C[A[j]]-1] = A[j];
C[A[j]] = C[A[j]] – 1;

Max-Heapify[2](A, i, lon)
{
 izq = 2*i;
 der = 2*i + 1;
 if (izq ≤ lon && A[izq] > A[i])
 mayor = izq
else
 mayor = i
if (der ≤ lon && A[der] > A[mayor])
{
 mayor = der;
 if (mayor <> i)
 swap(A,i,mayor)
 Max-Heapify(A, mayor, long)
}
}
--------------------
Build-Max-Heap(A, lon)
{
 for i = floor(lon/2) downto 1 do
 Max-Heapify(A, i)
}
--------------------
Heap-Increase-Key(A, i, key)
{
A[i]=key;
while (i>1 && a[padre(i)] < a[i])
{
Swap(A,i,padre(i));
i = padre(i)
}
}

You might also like