Question: SORTING ARRAY To sort we need: Ability to compare elements Ability to swap elements 0 1 2 3 4 5 6 7 8 9 0

SORTING ARRAY

To sort we need:

  1. Ability to compare elements
  2. Ability to swap elements

0

1

2

3

4

5

6

7

8

9

0

1

2

4

9

7

8

77

88

67

Here is a type of sort:

Objective: sort items in order of smallest to largest.

  1. Find the smallest element in the active portion of the array
  2. Swap smallest element with first element in active portion of the array
  3. Reduce active portion of array by one
  4. Repeat until complete

He is some pseudocode that shows how this can be accomplished.

// control the active portion of the array

For(int arr_start=0; arr_start < SIZEOFARRAY; arr_start ++){

// find the smallest element in active portion

Smallest = array_name[arr_start]; // assumed smallest

Index_of_smallest = arr_start;

For(j= arr_start ;j< SIZEOFARRAY; j++){

// get smallest

If array_name[j] < smallest{

Smallest = array_name[j]

Index_of_smallest = j

}

}

// swap smallest with first element in active portion of array

Temp = array_name[arr_start]

array_name[arr_start] = array_name[index_of_smallest]

array_name[index_of_smallest] = Temp

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!