Skip to content

Instantly share code, notes, and snippets.

@s3f
Created December 29, 2016 22:57
Show Gist options
  • Save s3f/26d6b7ccd38c9e0ff3fdab7bbb1c819e to your computer and use it in GitHub Desktop.
Save s3f/26d6b7ccd38c9e0ff3fdab7bbb1c819e to your computer and use it in GitHub Desktop.
Chapter 23 - Sorting Data created by s3f - https://repl.it/EvqV/12
/* Bubble Sort
The order you put values in can be either ascending (low to high) or descending (high to low)
In a bubble sort, the sort's float up the list each time a pass is made through the data.
The program below generates 10 random numbers and then sorts them
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
main()
{
int ctr, inner, outer, didSwap, temp;
int nums[10];
time_t t;
srand(time(&t));
// First step is to fill the array with random numbers from 1 -100
for (ctr = 0; ctr < 10; ctr++)
{
nums[ctr] = (rand() % 99) + 1;
}
// Now list the array as it currently is before sorting
puts("\nHere is the list before the sort");
for (ctr = 0; ctr < 10; ctr++)
{
printf("%d\n", nums[ctr]);
}
// Sort the array
for (outer = 0; outer < 9; outer++)
{
didSwap = 0; // Becomes 1 (true) if list is not ordered
for (inner = outer; inner < 10; inner++)
{
if (nums[inner] < nums[outer])
{
temp = nums[inner];
nums[inner] = nums[outer];
nums[outer] = temp;
didSwap = 1;
}
}
if (didSwap == 0)
{
break;
}
} // Now list the array as it curently is after sorting
puts("\nHere is the list after the sort");
for (ctr = 0; ctr < 10; ctr++)
{
printf("%d\n", nums[ctr]);
}
return (0);
}
Now the following program below searches a sorted list of customers ID's in order to get credit totals
*/
#include <stdio.h>
main()
{
int ctr; // loop counter
int idSearch; // customer to look for aka the key
int found = 0; // 1(true) if customer is found
// Defines the 10 elements in each of the parallell arrays
int custID[10] = { 313, 453, 502, 101, 892, 475, 792, 912, 343, 633 };
float custBalance[10]
= { 0.00, 45.43, 71.23, 301.56, 9.08, 192.41, 389.00, 229.67, 18.31, 59.54 };
int tempID, inner, outer, didSwap, i; // For sorting purposes
float tempBalance;
// First sort the arays by customer ID
for (outer = 0; outer < 9; outer++)
{
didSwap = 0; // Becomes true if list is not yet ordered
for (inner = outer; inner < 10; inner++)
{
if (custID[inner] < custID[outer])
{
tempID = custID[inner]; // Must switch both arrays or they are no longer linked
tempBalance = custBalance[inner];
custID[inner] = custID[outer];
custBalance[inner] = custBalance[outer];
custID[outer] = tempID;
custBalance[outer] = tempBalance;
didSwap = 1; // True because a swap took place
}
}
if (didSwap == 0)
{
break;
}
}
// Now we interact with the user looking to find a balance
printf("\n\n*** Customer Balance Lookup ***\n");
printf("What is the customer number? ");
scanf(" %d", &idSearch);
// Now look for the ID in the array
for (ctr = 0; ctr < 10; ctr++)
{
if (idSearch == custID[ctr]) // Do they match
{
found = 1; // Yes, match flag is set to TRUE
break; // No need to keep looping
}
if (custID[ctr] > idSearch) // No need to keep searching
{
break;
}
}
// Once the loop was completed, the ID was either found (1) or not
if (found)
{
if (custBalance[ctr] > 100)
{
printf("\n** That customer's balance is $%.2f\n", custBalance[ctr]);
printf("No additional credit.\n");
}
else // Balance is less than 100.00
{
printf("\n**The customer's credit is good!\n");
}
}
else // The ID was not found
{
printf("** YOu have entered an incorrect customer ID.");
printf("\n ID %3d was not found in the list. \n", idSearch);
}
return (0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment