C++ :: Sorting With Selection / Insertion And Bubble Sort

May 4, 2014

This program using the selection, insertion, and bubble sorts. The program needs to be able to do the following:

1. Create an array of 1000 population records when the array object is instantiated. Call it unSorted.

2.Open the file called "Population.csv" (on the portal) and invoke a function that loads the population data into the array.

3.Create a second array of 1000 elements that will be used to sort the data using the different algorithms. Name is sortedArray.

4.Write a function that will copy unSorted into sortedArray and execute that function.

5.Using a function, display the unsorted array.

6.Invoke the insertionSort () function that will sort the sortedArray using the insertion sort algorithm. Sort the population data on the rank field in ascending order. Alternatively, you can sort in descending order on population.

7.Using the display function, display sortedArray.

8.Display the number of iterations it took to do the sort using this algorithm.

9.Repeat steps 4-8 for the selection and bubble sort algorithms.

Here is my code so far:

Code:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
void loadArray (int unSorted[], int s);
void displayArray (const int num [], int size);

[Code] .....

Here is a few lines from the Population.csv file contents:

Code:
Alabama,Baldwin County,140415,389
Alabama,Blount County,51024,908
Alabama,Calhoun County,112249,477
Alabama,Colbert County,54984,858
Alabama,Cullman County,77483,653
Alabama,Dale County,49129,927
Alabama,Dallas County,46365,974
Alabama,DeKalb County,64452,753

I'm not sure how to load the data from the file into the array properly, I attempted this. I also don't know how to copy the unSorted into sortedArray.

View 14 Replies


ADVERTISEMENT

C/C++ :: Bubble Sort And Selection Sort?

Feb 19, 2014

You will write a program that uses a multidimensional array having 3 rows and 8 columns and sorts each of the rows using both a bubble sort and a selection sort.

You must declare the array inside of main. You will have a for loop containing the calls to bubbleSort and selectionSort. You need to pass into function bubbleSort and selectionSort the following: 1) each column of the multidimensional array, 2) the size of the column, and 3) a particular row number of the multidimensional array to be used for printing out the "pass" shown on the following pages.

I keep getting an error that the identifier for bubbleSort and selectionSort is not found. (Error C3861) Also, I feel like I'm missing something in int main() to get it to sort properly.

# include <iostream>
using namespace std;
int main()
{

[Code].....

View 14 Replies View Related

Visual C++ :: Multidimensional Array Having 3 Rows And 8 Columns - Bubble And Selection Sort

Feb 19, 2014

You will write a program that uses a multidimensional array having 3 rows and 8 columns and sorts each of the rows using both a bubble sort and a selection sort.

You must declare the array inside of main. You will have a for loop containing the calls to bubbleSort and selectionSort. You need to pass into function bubbleSort and selectionSort the following: 1) each column of the multidimensional array, 2) the size of the column, and 3) a particular row number of the multidimensional array to be used for printing out the "pass" shown on the following pages.

I keep getting an error that the identifier for bubbleSort and selectionSort is not found. (Error C3861)

Also, I feel like I'm missing something in int main() to get it to sort properly.

Code:
# include <iostream>
using namespace std;
int main() {
const int SIZE1 = 3;
const int SIZE2 = 8;
int arr [SIZE1][SIZE2] = { { 105, 102, 107, 103, 106, 100, 104, 101 },

[Code] ....

View 1 Replies View Related

C :: Using Insertion Sort For Sorting Database By Age

Apr 29, 2014

I am trying to sort a student database by age,but i am not sure whats wrong i think it has to deal with tmpstudent variable.

Code:
void insertion_sort(StudentDB *db) {
int i;
for (i = 0; i < db->num; ++i) {
int j = i - 1;
int val = db->records[i].age;

[Code] .....

View 1 Replies View Related

C++ :: Sorting Golfers Using Bubble Sort

Oct 29, 2013

I have to sort golfers using bubble sort. Here my code:

#include <iostream>
#include <cstdio>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;
void tellUser();
double calcAvg( int, int, int, int, int &);
void swap( int &num1, int &num2);

[Code] ....

View 2 Replies View Related

C++ :: Sorting And Searching With Bubble Sort?

May 15, 2014

I'll make a program that consists of "sorting and searching." To cope with the task, it should contain this: Create a field containing 50 random numbers, sort them by a method sort according to Bubble sort and then out the field before and after sorting. [URL]

View 1 Replies View Related

C :: Sorting Strings Using Selection Sort Method?

Dec 7, 2013

I am trying to write a program to sort the characters in a word alphabetically. For example, if you input 'what', the computer will sort it into 'ahtw'. But, it fails to work. I didn't know why.

Code:

#include <stdio.h>
#include <string.h>
main() {

[Code].....

View 8 Replies View Related

C++ :: Sorting Randomized Array Of Integers Using Bubble Sort Algorithm?

Jun 26, 2013

This program is sorting a randomized array of integers using the bubblesort algorithm.

I am trying to modify n correct the source code,so that the swapping of two values will be done by a function called swap values() by using call-by-reference but function should have as arguments only the two array elements that must be exchanged. (Note: do not pass the whole array to the function!) .We consider an array with the first element containing the number of elements in the array, followed by 10 randomly initialized integers (elements).

The code must sort the 10 elements in ascending order.

Compile: g++ -Wall sorting.cpp -o sorting
*/
#include <iostream>
#include <stdlib.h>
using namespace std;
int main() {
const int SIZE=10;

[Code] .....

View 1 Replies View Related

C++ :: Sorting Array Of Numbers Into Ascending Order - Bubble Sort Keeps Crashing

Oct 29, 2014

I am trying to write a program which will sort an array of numbers into ascending order, here is my code

#include<iostream>
#include<cmath>
using namespace std;
int main(){
int array[]={1, 5, 17, 3, 75, 4, 4, 23, 5, 12, 34, 34, 805, 345, 435, 234, 6, 47, 4, 9, 0, 56, 32, 78};

[Code] .....

This compiles fine but when I run the .exe for the first time an error message comes up saying program has stopped working. If I run the program again without recompiling it seems to work as expected.

View 2 Replies View Related

C :: Concept Of Insertion Sort - Verification

Apr 7, 2014

Can validate if my code implements the concept of insertion sort. The program is executing successfully. Just need a verification!

Code:

int i,j,k,a[100],n,num,min,max,temp;
printf("Enter array size: ");
scanf(" %d",&n);
printf("
Enter the numbers:");
for(i=0;i<n;i++)
scanf(" %d", &a[i]);

[Code] ....

View 9 Replies View Related

C/C++ :: Two Dimensional Array Using Insertion Sort?

Apr 3, 2014

I want to use two dimensional array by insertion sort or quick sort

View 2 Replies View Related

C :: Sorting A Linked List Upon Node Insertion

Aug 30, 2013

While I know that linked lists seem to be a fairly common problem area among beginner C programmers, most examples that I have come across abstract the sorting of a linked list to a separate function which is sadly not what I am trying to do.

The code below is my attempt to have the nodes inserted into their correct place so that once all nodes have been inserted they are already in a sorted order.

Code:

int main(int argc, char *argv[])
{
struct node_t* dict_head;
struct node_t* traversor;
struct node_t* newnode;
int list_size = 0, insert_key, search_key, delete_key, x;
char insert_word[WORDLEN];
/*Opening the dictionary file provided by the command line argument */
FILE *fp;
fp = fopen(argv[1], "r");

[Code]....

The problem is as follows. When you provide it with input in which the first word scanned is any other word than that which would be placed at the front of the list, it works as intended. For example.

7 world
0 ant
3 kodak
1 best
6 the
2 is

Produces ant -> best->is->kodak->best->world

However, swapping ant and world in the above input gives:
world->best->is->kodak->best->world

In regards to why I have my head node set as a node without a word or a key, it was suggested that I make it so that the first node inserted is set to be the head of the list and then changed as sorting required, yet this caused only additional problems.

View 10 Replies View Related

C :: Insertion Sort Function Crashing If 8 Or More Values?

Jan 15, 2015

This program I'm working on accepts an array size from the user, prompts the user to store that many integers, sorts them from smallest to largest, and then searches for duplicates with a simple for loop. The ultimate goal of the assignment was to display duplicates in an array, and the rest of the functions are just how I decided to reach that goal.

Anyway, my program crashes if I choose an array size larger than 7. It sorts and displays duplicates perfectly with 7 or fewer arguments.

The exact moment it crashes is after I enter the final value it prompts me for, so it appears my inputsize() function and my inputarray() function are working, and the error may be in the arrsort() function. Code is below:

Code:
#include <stdio.h>
int funcinputsize(int);
void funcinputarray(int [], int size);
void funcarrsort(int [], int size);
void funcdupe(int [], int size);

[Code] ...

View 4 Replies View Related

C :: Insertion Sort In Ascending Order Not Working

Jun 20, 2014

I am having trouble sorting out a list of names in c. I have code for sorting the names, but when I go to print them out they still are in the same order as they were at the beginning so something isnt right. So the function that I need is the sort_data function.

Code:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_STRING_LEN 25
void insert_data(char **strings, const char* filename, int size);
void allocate(char ***strings, int size);

[Code] ....

The list that I am reading in is as follows:

matt
susan
mark
david
aden
phil
erik
john
caden
mycah

So I need to get this list in alphabetical order, but when I run my code and print out this list after I run the sort function, they are still in this order.

View 5 Replies View Related

C++ :: Insertion Sort Program - Runtime Error

Apr 11, 2013

I am trying to run this program for Insertion Sort, But some how I am getting some problem:

#include<iostream>
int main(){
int i,j,key,n;
scanf("%d",&n);
int a[n];

[Code] .....

Error
In function 'int main()':
Line 10: error: ISO C++ forbids variable-size array 'a'
compilation terminated due to -Wfatal-errors.

View 2 Replies View Related

C++ :: Insertion Sort Algorithm With Comparison Counter

Nov 7, 2013

So I have an insertion sort function implemented that sorts through an array, but I'm having a problem showing the correct number of comparisons to work.

Each time I'm checking a value with another, the counter should update.

For instance, having an array of 10 elements going from 10-1 should give 45 comparisons, but I'm getting 54 comparisons.

void insertionSort(int a[], int& comparisons, const int& numOfElements) {
int j, value;
for (int i = 1; i < numOfElements; i++) {
value = a[i];
for (j = i - 1; j >= 0 && a[j] > value; j--)

[Code] .....

View 3 Replies View Related

C++ :: How To Use Bubble Sort

Feb 20, 2012

how can i use my function now in the main ... after i put the numbers for example i want them to be sorted in ascending or descending order

PHP Code:

# include <iostream>
using namespace std;
int main () {
    int array [10]; 
    cout << " enter numbers : ";
    for ( int i=0; i<10; i++)

[code]....

View 4 Replies View Related

C++ :: Bubble Sorting Of Strings

Sep 20, 2013

I have to read data from a text file, load it into an array and then bubble sort it! Here is my code:

Code:
#include <iostream>
#include <string>
#include <fstream>

[Code]....

Basically what i am trying to do is that sort the names of 10,000 movies and then write those names to a text file. It gives an error in the nested loop by underlining arr and tells that no suitable conversion function from std::string to const char* exists.

View 5 Replies View Related

C++ :: How To Optimize Bubble Sort

Mar 24, 2013

How to optimize this bubble sort program?

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>
#include <cstdlib>
#include <iostream>
#include <ctime>

using namespace std;
int main() {
long *A;
A = new long[100000000];

[Code]] .....

View 2 Replies View Related

C :: Selection Sorting A 2D Array

Feb 13, 2013

Selection sorting a 2D array . Let's say i have an array like

1 2 3 4 //4 elements
1 2 // 2 elements
1 2 3 4 5 //5 elements
1 2 3 //3 elements
1 //1 element

And I want to do a selection sort it in descending order which the row with 5 elements will come first then 4 then 3 and so on. So that it would look like this

1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

Code:
void selectionSortDescending(int list[MAX_ROW], int size){
int temp;
int walk;
int curr;
int large; // index of the largest element

for (walk = 0; walk < size - 1; walk++)

[Code] ....

View 7 Replies View Related

C++ :: Bubble Sorting With Function Templates

Jun 21, 2013

i'm new to C++ i came across this bubblesort program earlier relating to class templates i was wondering how to make this into simple function templates

#include<iostream>
#include<iomanip.h>
#include<conio.h>
#include<stdio.h>
using namespace std;
template <class t>
class bubble

[Code] ....

View 3 Replies View Related

C++ :: Bubble Sorting A Classed Array

May 8, 2014

I'm really struggling with bubble sorting a classed array(an inventory). I understand bubble sorting in theory and implementing it into a static array makes sense. When I get to doing it in a classed array then I struggle. I need to sort the weight by bubble sort (then selection sort for name, and insertion sort the cost; but I want to tackle one problem at a time.

Here is the code:

#include <iostream>
#include <iomanip>
#include <conio.h>
#include "Inventory.h"
#define MAX_REC 10
using namespace std;
//Class to hold inventory variables/funcitons

[Code] ......

View 6 Replies View Related

C++ :: Sorting Array With Double Bubble?

Feb 3, 2013

i wanted to sort an array with double bubble sort but it didnt work!

my code is this:

void doubleBubbleSort(int *array, int length) {
int i,j;
for(i=0; i<length--;i++) {
if(array[i]>array[i++])
for(j=i;j>=0&&(array[j]>array[j++]);j--)
std::swap(i,j);

[code].....

how to get time like 1.56 seconds,too!

View 3 Replies View Related

C :: Bubble Sort With Random Numbers

Nov 6, 2014

i'm trying to fill an array with random numbers and then sort them via bubblesort. it seems to work so far. the problem is, that i seem to get the same numbers for the same input. somehow the randomness isn't working.

Code:

#include <stdio.h>
#include <stdlib.h>
int main() {
int a, b, c, d, e, f;
}

[code]....

View 3 Replies View Related

C++ :: Trace Bubble Sort Using Integers

Mar 6, 2014

Trace the bubble sort using the following integers,which represent the elements in an array.

5,7,3,8,6,7,3

View 1 Replies View Related

C++ :: Bubble Sort - Put In Descending Order

Jan 22, 2014

so my program reads a file type which looks like this...

Michelle 71
Marcie 99
David 42
Rebecca 83
Jonathan 79
Matthew 77
Rose 7
Melanie 75
Kimberly 73
Roger 74
Scott 76
Bradley 77
Drextell 10
Heidi 70
Alan 68
Pearl 13
Jeanne 43
Heber 55

Here is whats in the header of the class

class StudentStat {
private:
int Size;

[Code].....

So as of right now the names are stored in a Names string array and the scores are saved in a Score int array. So my question is where do I begin with my bubble sort? I need it to put the scores in descending order so from greatest score to lowest but I need the Names in the string array to still be connected to the number from the list. Never done a bubble sort before so not sure where to begin.

View 1 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved