Selection Sort
Selection Sort, Ascending Order, Using Minimum
#include<stdio.h>
#define TOTAL_SIZE 10
void print_array(int *array, int current_size);
void selection_asc_min(int *array, int current_size);
int main(){
int a[TOTAL_SIZE] = {4, 6, 1, 8, 5, 2, 0, 3, 9, 7}, current_size = 10;
print_array(a, current_size);
selection_asc_min(a, current_size);
print_array(a, current_size);
return 0;
}
void selection_asc_min(int *array, int current_size){
int i, j, min_index, temp;
for(i = 0; i < current_size; i++){
min_index = i;
for(j = i+1; j < current_size; j++){
if(array[j] < array[min_index]){
min_index = j;
}
}
temp = array[i];
array[i] = array[min_index];
array[min_index] = temp;
}
}
void print_array(int *array, int current_size){
int i;
if(current_size==0){
printf("Array is Empty\n");
}
else{
printf("Array Elements are: ");
for(i=0; i<current_size; i++){
printf("%d ", array[i]);
}
printf("\n");
}
}
Selection Sort, Ascending Order, Using Maximum
#include<stdio.h>
#define TOTAL_SIZE 10
void print_array(int *array, int current_size);
void selection_asc_max(int *array, int current_size);
void selection_asc_max2(int *array, int current_size);
int main(){
int a[TOTAL_SIZE] = {4, 6, 1, 8, 5, 2, 0, 3, 9, 7}, current_size = 10;
print_array(a, current_size);
selection_asc_max(a, current_size);
//selection_asc_max2(a, current_size);
print_array(a, current_size);
return 0;
}
void selection_asc_max(int *array, int current_size){
int i, j, max_index, temp;
for(i = current_size-1; i > 0 ; i--){
max_index = i;
for(j = 0; j < i; j++){
if(array[j] > array[max_index]){
max_index = j;
}
}
temp = array[i];
array[i] = array[max_index];
array[max_index] = temp;
}
}
void selection_asc_max2(int *array, int current_size){
int i, j, max_index, temp;
for(i = 0; i < current_size ; i++){
max_index = 0;
for(j = 0; j < current_size-i; j++){
if(array[j] > array[max_index]){
max_index = j;
}
}
temp = array[current_size-i-1];
array[current_size-i-1] = array[max_index];
array[max_index] = temp;
}
}
void print_array(int *array, int current_size){
int i;
if(current_size==0){
printf("Array is Empty\n");
}
else{
printf("Array Elements are: ");
for(i=0; i<current_size; i++){
printf("%d ", array[i]);
}
printf("\n");
}
}
Selection Sort, Descending Order, Using Minimum
#include<stdio.h>
#define TOTAL_SIZE 10
void print_array(int *array, int current_size);
void selection_desc_min(int *array, int current_size);
void selection_desc_min2(int *array, int current_size);
int main(){
int a[TOTAL_SIZE] = {4, 6, 1, 8, 5, 2, 0, 3, 9, 7}, current_size = 10;
print_array(a, current_size);
selection_desc_min(a, current_size);
//selection_desc_min2(a, current_size);
print_array(a, current_size);
return 0;
}
void selection_desc_min(int *array, int current_size){
int i, j, min_index, temp;
for(i = current_size-1; i >= 0 ; i--){
min_index = i;
for(j = 0; j < i; j++){
if(array[j] < array[min_index]){
min_index = j;
}
}
temp = array[i];
array[i] = array[min_index];
array[min_index] = temp;
}
}
void selection_desc_min2(int *array, int current_size){
int i, j, min_index, temp;
for(i = 0; i < current_size ; i++){
min_index = 0;
for(j = 0; j < current_size-i; j++){
if(array[j] < array[min_index]){
min_index = j;
}
}
temp = array[current_size-i-1];
array[current_size-i-1] = array[min_index];
array[min_index] = temp;
}
}
void print_array(int *array, int current_size){
int i;
if(current_size==0){
printf("Array is Empty\n");
}
else{
printf("Array Elements are: ");
for(i=0; i<current_size; i++){
printf("%d ", array[i]);
}
printf("\n");
}
}
Selection Sort, Descending Order, Using Maximum
#include<stdio.h>
#define TOTAL_SIZE 10
void print_array(int *array, int current_size);
void selection_desc_max(int *array, int current_size);
int main(){
int a[TOTAL_SIZE] = {4, 6, 1, 8, 5, 2, 0, 3, 9, 7}, current_size = 10;
print_array(a, current_size);
selection_desc_max(a, current_size);
print_array(a, current_size);
return 0;
}
void selection_desc_max(int *array, int current_size){
int i, j, max_index, temp;
for(i = 0; i < current_size; i++){
max_index = i;
for(j = i+1; j < current_size; j++){
if(array[j] > array[max_index]){
max_index = j;
}
}
temp = array[i];
array[i] = array[max_index];
array[max_index] = temp;
}
}
void print_array(int *array, int current_size){
int i;
if(current_size==0){
printf("Array is Empty\n");
}
else{
printf("Array Elements are: ");
for(i=0; i<current_size; i++){
printf("%d ", array[i]);
}
printf("\n");
}
}
#include<stdio.h>
void print_array(int a[], int size);
void selection_sort_descending(int a[], int size);
void selection_sort_descending2(int a[], int size);
void selection_sort_ascending(int a[], int size);
void selection_sort_ascending2(int a[], int size);
void selection_sort_ascending_recursion(int a[], int start, int size);
void selection_sort_descending_recursion(int a[], int size);
int main(){
int a[10] = {1, 4, 3, 5, 6, 7, 2, 8, 9, 0}, size = 10;
int i, j, max_pos, temp;
print_array(a, size);
//selection_sort_ascending(a, size);
//selection_sort_ascending2(a, size);
//selection_sort_ascending_recursion(a, 0, size);
selection_sort_descending_recursion2(a, size);
//selection_sort_descending(a, size);
//selection_sort_descending2(a, size);
print_array(a, size);
return 0;
}
void selection_sort_descending_recursion(int a[], int size){
int i, j, min_pos, temp;
if(size==0){
return;
}
min_pos = 0;
for(j=0; j<size; j++){
if(a[j]<a[min_pos]){
min_pos = j;
}
}
temp = a[size-1];
a[size-1] = a[min_pos];
a[min_pos] = temp;
selection_sort_descending_recursion(a, size-1);
}
void selection_sort_descending_recursion2(int a[], int size){
if(size){
int i, j, min_pos, temp;
min_pos = 0;
for(j=0; j<size; j++){
if(a[j]<a[min_pos]){
min_pos = j;
}
}
temp = a[size-1];
a[size-1] = a[min_pos];
a[min_pos] = temp;
selection_sort_descending_recursion(a, size-1);
}
}
void selection_sort_ascending_recursion(int a[], int start, int size){
int i, j, min_pos, temp;
if(start == size){
return;
}
min_pos = start;
for(j=start+1; j<size; j++){
if(a[j]<a[min_pos]){
min_pos = j;
}
}
temp = a[start];
a[start] = a[min_pos];
a[min_pos] = temp;
selection_sort_ascending_recursion(a, start+1, size);
}
/*
{1, 4, 3, 5, 6, 7, 2, 8, 9, 0} {0, 4, 3, 5, 6, 7, 2, 8, 9, 1}
{4, 3, 5, 6, 7, 2, 8, 9, 1} {1, 3, 5, 6, 7, 2, 8, 9, 4}
{3, 5, 6, 7, 2, 8, 9, 4} {2, 5, 6, 7, 3, 8, 9, 4}
{5, 6, 7, 3, 8, 9, 4} {3, 6, 7, 5, 8, 9, 4}
{6, 7, 5, 8, 9, 4} {4, 7, 5, 8, 9, 6}
{7, 5, 8, 9, 6} {5, 7, 8, 9, 6}
{7, 8, 9, 6} {6, 8, 9, 7}
{8, 9, 7} {7, 9, 8}
{9, 8} {8, 9}
{9} {9}
{}
*/
Last updated