We are going to discuss :
- Insertion
- Selection
- Bubble
- Shell
- Quick
- Merge
- Heap
void insertionSortArray(int arr[], int SIZE)
{
int i, j, value, done;
for(i = 0; i < SIZE; i++)
{
value = arr[i];
j = i - 1;
done = 0;
do
{
if(arr[j] > value)
{
arr[j + 1] = arr[j];
j--;
if(j < 0) done = 1;
}
else done = 1;
} while(!done);
arr[j + 1] = value;
}
}
/* ************************************ */
void selectionSortArray(int arr[], int SIZE)
{
int min, i, j, tmp;
for(i = 0; i < SIZE; i++)
{
min = i;
for(j = i + 1; j < SIZE; j++)
if(arr[j] < arr[min]) min = j;
tmp = arr[i];
arr[i] = arr[min];
arr[min] = tmp;
}
}
/* ************************************ */
void bubbleSortArray(int arr[], int SIZE)
{
int i, tmp;
for (i = 0; i < SIZE; i++)
{
if((i < SIZE - 1) && (arr[i] > arr[i + 1]))
{
tmp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = tmp;
i = -1;
}
}
}
/* ************************************ */
void shellSortArray(int arr[], int SIZE)
{
int i, j, k, m, mid;
for(m = SIZE / 2; m > 0; m = m / 2)
{
for(j = m; j < SIZE; j++)
{
for(i = j - m; i >= 0; i = i - m)
{
if(arr[i + m] >= arr[i]) break;
else
{
mid = arr[i];
arr[i] = arr[i + m];
arr[i + m] = mid;
}
}
}
}
}
{
int i, j, value, done;
for(i = 0; i < SIZE; i++)
{
value = arr[i];
j = i - 1;
done = 0;
do
{
if(arr[j] > value)
{
arr[j + 1] = arr[j];
j--;
if(j < 0) done = 1;
}
else done = 1;
} while(!done);
arr[j + 1] = value;
}
}
/* ************************************ */
void selectionSortArray(int arr[], int SIZE)
{
int min, i, j, tmp;
for(i = 0; i < SIZE; i++)
{
min = i;
for(j = i + 1; j < SIZE; j++)
if(arr[j] < arr[min]) min = j;
tmp = arr[i];
arr[i] = arr[min];
arr[min] = tmp;
}
}
/* ************************************ */
void bubbleSortArray(int arr[], int SIZE)
{
int i, tmp;
for (i = 0; i < SIZE; i++)
{
if((i < SIZE - 1) && (arr[i] > arr[i + 1]))
{
tmp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = tmp;
i = -1;
}
}
}
/* ************************************ */
void shellSortArray(int arr[], int SIZE)
{
int i, j, k, m, mid;
for(m = SIZE / 2; m > 0; m = m / 2)
{
for(j = m; j < SIZE; j++)
{
for(i = j - m; i >= 0; i = i - m)
{
if(arr[i + m] >= arr[i]) break;
else
{
mid = arr[i];
arr[i] = arr[i + m];
arr[i + m] = mid;
}
}
}
}
}
No comments:
Post a Comment