Hello,
I'm trying ultimately to do a radix sort. I'm going with the implementation using two tables. One will initially hold the unsorted values then hold the partially sorted values thereafter. The other will be an array of linked lists which will be where the radix sort is done. Here is my code thus far:
#ifndef radix_h
#define radix_h
class radix {
private:
struct node {
int num ;
node * next ;
};
int * unsorted ;
int unsorted_size ;
node ** sorted ;
int sorted_size ;
int max_place ;
int num_elements;
public:
// Default constructor
radix ( ) ;
// ===================
// Prints the unsorted list=======
void print_unsorted_list ( ) ;
// ===============================
// Prints the sorted list ======
void print_sorted_list ( ) ;
// =============================
// Sets the unsorted list size =============
void set_unsorted_size ( int new_val ) ;
// =========================================
// Sets the sorted list size =============
void set_sorted_size ( int new_val ) ;
// =======================================
// Gets the unsorted list size
int get_unsorted_size ( ) ;
// ============================
// Gets the sorted list size
int get_sorted_size ( ) ;
// ==========================
// Gets the new size of the array to store the values
int get_new_sorted_size ( int place ) ;
// ==================================================
// Gets a value in the ones, tens... place of digit =======
int get_digit_place_value ( int digit , int place ) ;
// ========================================================
// Gets the max place value
int get_max_place ( ) ;
// ========================
// Sets the max place value ================
void set_max_place_value ( int digit ) ;
// =========================================
// Gets the maximum place value of digit, such as ones, tens ...
int get_place_value ( int digit ) ;
// =============================================================
// Inserts a number into the unsorted list ======
void insert_into_unsorted ( int to_insert ) ;
// ==============================================
// Inserts a number into the sorted list ==================
void insert_into_sorted ( int to_insert , int place ) ;
// ========================================================
// Gets the number at the index from the unsorted list
int get_unsorted_val ( int index ) ;
// ===================================================
// Gets the node at the index from the sorted list
void print_sorted_val ( int index ) ;
// ===============================================
// Sets the total number of elements from a file
void set_num_elements ( int new_num ) ;
// =============================================
// Gets the total number of elements
int get_num_elements ( ) ;
// =================================
// Creates a new sorted array ===============
void create_new_sorted ( int new_size ) ;
// ==========================================
// Zeros out an array ==========
void zero_sorted_array ( ) ;
// =============================
// Puts the contents of the sorted_array back into the unsorted array
void back_in_unsorted ( ) ;
// ==================================================================
};
#endif // radix_h
It appears to be crashing during void insert_into_sorted ( int to_insert , int place ) ; and I'm not sure why.
void radix::insert_into_sorted ( int to_insert , int place ) {
node * current = sorted [ get_digit_place_value ( to_insert , place ) , ] ;
while ( current != NULL ) {
current = current->next ;
}
current->num = to_insert ;//current is NULL
}
void radix::insert_into_sorted ( int to_insert , int place ) {
node * current = sorted [ get_digit_place_value ( to_insert , place ) , ] ;
while ( current != NULL ) {
current = current->next ;
}
current = new node ;
current->num = to_insert ;
}