Computer Science 1511
Computer Science II

Laboratory Assignment 12
Sorting Arrays
Due at the end of Lab


Sorting is the process by which a set of n elements are arranged in an order based on the values in the array. An array may be arranged in ascending (smallest to largest) or descending (largest to smallest) order. There are several different algorithms that can be used for sorting arrays including:

  Selection Sort
  Insertion Sort
  Merge Sort

Selection Sort is a very basic algorithm used in sorting. If we want to sort the elements in ascending order, selection sort works by selecting the element with the minimum value in the unsorted portion of the array and inserting it into its proper place in the sorted portion. This process is repeated until the whole array is sorted. We follow the same procedure for sorting the array in descending order except that we select the element with the maximum value and insert it into the sorted portion.

We can sort an array of integers based on their numerical values or can an array of characters based on their ASCII values.

In this lab we are going to get some experience with sorting the elements in an array.

The Program

The following Program reads in values into an array and sorts them in descending order. This sorting is done by the function named sortdescend. The program also contains a function to "unsort" the array once it is sorted called unsort.

To this program we want to add two processes:

  1. Code to sort the array of integers in ascending order.
  2. Code to sort the array of characters in descending order.
#include <stdio.h>
#include <stdlib.h>
#define ASIZE 10

int main() {
  int A[ASIZE];
  char C[ASIZE];
  int i;
  void print(int A[], int N);  /* To Print out the array */
  void sortdescend(int A[], int N);
  void unsort(int A[], int N);
  /* Scan the elements into the array */
  for (i = 0; i < ASIZE; i++) {
     printf("Enter int element %d: ", i);
     scanf("%d", &A[i]);
  /* Print the values read into the array */
  printf("\nThe initial int array is \n");

  /* Sorts the array using selection sort algorithm */
  /* Prints the sorted array */
  printf("\nThe sorted (descending order) array is \n");
  /* Unsorts the sorted array in a random fashion */
  /* Prints the unsorted array */
  printf("\nAfter unsorting the array is \n");

  /* Add code here to sort the int array in ASCENDING ORDER */

  /* Prints the sorted array */
  printf("\nThe sorted (ascending order) array is \n");
  /* Read in the characters in to the char array */
  for (i = 0; i < ASIZE; i++) {
    printf("Enter character element %d: ", i);
    scanf(" %c", &C[i]);
  /* Print the initial ordering of the array */
  printf("\nThe initial char array is \n");
  for (i = 0; i < ASIZE; i++)
     printf (" %c ", C[i]);

  /* Add code here to sort the char array in DESCENDING ORDER */


  /* Print the array after sorting */
  printf("\nThe sorted char array is \n");
  for (i = 0; i < ASIZE; i++)
     printf (" %c ", C[i]);

  printf("Press return to finish.\n");

  return 0;
void sortdescend (int A[], int N) {
  int i, j, max, temp;
  for (i = 0; i < N; i++) {
    max = i;
    for (j = i+1; j <  N; j++)
       if (A[j] > A[max])
        max = j;

    temp = A[i];
    A[i] = A[max];
    A[max] = temp;

void unsort (int A[], int N) { /* Function that resets the array in some random order */
  int i;
  int rnd, temp;

  for(i = 0; i < N; i++) {
    rnd = rand() % N;
    temp = A[i];
    A[i] = A[rnd];
    A[rnd] = temp;

void print (int A[], int N) { /* Function to print the array elements */
  int i;
  for (i = 0; i < N; i++)
    printf (" %d ", A[i]);

What to turn in

Turn in a hard copy of your final program. Also, turn in a copy of output for your program showing that it works correctly.