CS157: Intro to C, Part II

Spring 2018

Sorting

See this page as a slide show

Searching and Sorting arrays

CS157 Sorting

Searching & Sorting methodologies

Linear Search

The easiest algorithm is a Linear Search:

Linear Search

#define SIZE 7
int arr[SIZE] = {1, 99, 25, 68, 92, 38, 74};
int key = 68;       // searching for this
int where = -1;     // Where we found it
for (int i=0; i<SIZE; i++) {
    if (arr[i] == key) {
        where = i;
        break;
    }
}
if (where == -1)
    printf("Sorry, %d not found in list\n", key);
else
   printf("%d found at position %d\n", key, where);
68 found at position 3

Binary Search

Binary search, on the other hand, is smarter. It takes advantage of the fact that the data that you’re searching is sorted already.

Binary Search

#define SIZE 7
int arr[SIZE] = {1, 25, 38, 68, 74, 92, 99};
int key = 68;       // searching for this
int low=0, mid, high=SIZE-1;
while (low <= high) {
    mid = (low+high) / 2;
    if (arr[mid] == key)
        break;
    else if (arr[mid] > key)
        high = mid-1;
    else
        low = mid+1;
}
if (low <= high)
   printf("%d found at position %d\n", key, mid);
else
    printf("Sorry, %d not found in list\n", key);
68 found at position 3

Sorting

Selection Sort

Selection Sort

Original: 3  9  6  1  2 
Smallest unsorted is 1; swap with first unsorted, 3: 1  9  6  3  2 
Smallest unsorted is 2; swap with first unsorted, 9: 1  2  6  3  9 
Smallest unsorted is 3; swap with first unsorted, 6: 1  2  3  6  9 
Smallest unsorted is 6; swap with first unsorted, 6: 1  2  3  6  9 

Swapping

temp = first;
first = second;
second = temp;

Insertion Sort

Insertion Sort

Original:  3    9    6    1    2  
After inserting 9:  3    9    6    1    2  
After inserting 6:  3    6    9    1    2  
After inserting 1:  1    3    6    9    2  
After inserting 2:  1    2    3    6    9  

Comparing Sorts

Bubble Sort

Bubble Sort

Bubbling largest element to the end of the list:

Original:  3    9    6    1    2  
After pass 1:  3    6    1    2    9  
After pass 2:  3    1    2    6    9  
After pass 3:  1    2    3    6    9  
After pass 4:  1    2    3    6    9  

It resembles selection sort, as the largest element travels to the end of the list. However, there’s no explicit selection step.

Bubble Sort

Bubbling smallest element to the front of the list:

Original:  3    9    6    1    2  
After pass 1:  1    3    9    6    2  
After pass 2:  1    2    3    9    6  
After pass 3:  1    2    3    6    9  
After pass 4:  1    2    3    6    9  

Bubble Sort

#define SIZE 7
int arr[SIZE] = {1, 99, 25, 68, 92, 38, 74};

for (int i=1; i<SIZE; i++)
    for (int j=0; j<SIZE-i; j++)
        if (arr[j] > arr[j+1])
            swap(arr, j, j+1);
for (int i=0; i<SIZE; i++)
    printf("%d\n", arr[i]);

User: Guest

Check: HTML CSS
Edit History Source

Modified: 2018-04-16T11:33

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2018 Colorado State University
CS Building