Here is the code for [B][COLOR="Red"]Insertion Sort, Bubble Sort and Selection Sort[/COLOR][/B] [B]Insertion Sort[/B] [CODE] #include <stdio.h> main() { int i,j,key; int a[5]={5,2,3,4,1}; for(i=1;i<5;i++) { key=a[i]; while(i>0 && a[i-1]>key) { j=a[i]; a[i]=a[i-1]; a[i-1]=j; --i; } } int k; for(k=0;k<5;k++) {printf("%d ", a[k]);} printf("\n"); } [/CODE] [B]Bubble Sort[/B] [CODE] #include <stdio.h> main() { int i,j,x; int a[5]={5,2,3,4,1}; for(i=0;i<5;i++) { for(j=i+1;j<5;j++) { if(a[i]>a[j]) { x=a[i]; a[i]=a[j]; a[j]=x; } } } int k; for(k=0;k<5;k++) {printf("%d ",a[k]);} printf("\n"); }[/CODE] [B]Selection Sort[/B] [CODE] #include <stdio.h> main() { int i,j,x,min,k; int a[5]={5,3,2,4,1}; for(i=0;i<5;i++) { min=i; for(j=i+1;j<5;j++) { if(a[min]>a[j]) { min=j; } x=a[min]; a[min]=a[j]; a[j]=x; } } …

Member Avatar
Member Avatar
+0 forum 18

Hello, for a school assignment I was supposed to write an insertion sort algorithm for a doubly linked list. As the feedback system of this particular course is rather weak (actually non-existent) I would like to ask you for your honest opinion about how well this is implemented, what could be improved, made more efficient, etc? Thanks in advance, Marcel #include <iostream> using namespace std; class elem{ public: int value; elem* prev; elem* next; elem(int e, elem* ptr1, elem* ptr2){ value = e; prev = ptr1; next = ptr2; }; }; class list{ private: elem* first; elem* last; int length; …

Member Avatar
Member Avatar
+0 forum 4

> The following letters are stored in an alphabet array: B, J, K, M, S, and Z. > Write and test a function named adlet(), which accepts the alphabet array and a new letter >as arguments, and then inserts the new letter in the correct alphabetical order in the array. void sortNewArray(char array[], char newLetter) { for (int i = 0; i < newLetter - 1; i++) { if (array[i] > array[i + 1]) { int temp = array[i + 1]; array[i + 1] = array[i]; array[i] = temp; i = -1; } } } void adlet(char array[], char newLetter) …

Member Avatar
Member Avatar
+0 forum 5

#include <cstdlib> #include <ctime> #include <iostream> using namespace std; int findSmallestRemainingElement (int array[], int size, int index); void swap (int array[], int first_index, int second_index); void sort (int array[], int size) { for ( int i = 0; i < size; i++ ) { int index = findSmallestRemainingElement( array, size, i ); swap( array, i, index ); } } int findSmallestRemainingElement (int array[], int size, int index) { int index_of_smallest_value = index; for (int i = index + 1; i < size; i++) { if ( array[ i ] < array[ index_of_smallest_value ] ) { index_of_smallest_value = i; } } …

Member Avatar
Member Avatar
+0 forum 1

Hi all, Does anyone know how to quickly (i.e. O(log n) time) insert items into a sorted list? I'd prefer an existing working function if available; I didn't see any appropriate ones in boost or stl. Some background: I'd like to keep a constant sized sorted list, and insert items into the list while maintaining order. The item at the tail of the list is discarded after every insertion to maintain a constant size. This is very easy to do in C# using the SortedList container class, but I need to use C++. Number of items will be a few …

Member Avatar
Member Avatar
+0 forum 2

Hi I have data to upload to a Mysql database and I need to get the time taken for each row to be inserted so that I can pass that number through to a progress bar. I have alreay tried accomplishing this by determining the number of rows affetced by the insertion then find the percentage of that number which is not the correct manner to do this. here is the code $result3=mysql_query("INSERT INTO dest_table.create_info SELECT * from Profusion.source_cdr") or die(mysql_error()); $progress=mysql_affected_rows(); // Total processes $total = $progress; // Loop through process for($i=1; $i<=$total; $i++){ // Calculate the percentage $percent …

Member Avatar
Member Avatar
+0 forum 3

Hi,I am having a hard time trying to do insertion sort on a singly link list containing single alphabets.I have spent hours doing this but still it wont work.Any help will be apprecited. Here is my code: # include <iostream> using namespace std; class node { public: char info; node *next; node(char e1,node *ptr=0) { info=e1; next=ptr; } }; class linklist { private: node *head; public: linklist() { head=0; } void addtohead(char e1) { head=new node(e1,head); } void Printlist() { node *temp=head; while(temp!=NULL) { cout<<temp->info<<endl; temp=temp->next; } } void insertionsort() { node *i,*j; char min; for(i=head->next;i!=NULL ;i=i->next) { min=i->info; for(j=i->next;j!=NULL …

Member Avatar
Member Avatar
+0 forum 4

Hello, I have been having quite a bit of trouble with this one. Can someone please tell me how many swaps it would require to sort the following array? {2, 4, 6, 3, 7, 1} I tried putting my counter in a couple of places and neither one of them add up to what I get by simply counting the swaps manually. My manual count is two. Thank you, Hank. /** The IntInsertionSorter class provides a public static method for performing an insertion sort on an int array. */ public class IntInsertionSorter2 { private static int count = 0; /** …

Member Avatar
Member Avatar
+0 forum 6

I have been told it is good to overload the stream extraction operator to load data from a file. Would I simple use this in my class: friend ifstream &operator>>( ifstream &input, Class &C ) { input >> C.variable1 >> C.variable2 >> C.variable3; return input; } What are the advantages of overloading the stream extraction operator to read data from a file? Also I am confused what to do in main. At the moment to read in the data I have for example: ifstream fin; fin.open("filename.txt"); if(fin.good()) { object1 = new Class[10]; for (int i=0; i<10; i++) { fin >> …

Member Avatar
Member Avatar
+0 forum 7

Hello dear ALL I am working with a form in vb.net which have the follwing attribute i.e BOOK TITLE(text box) , CATEGORY NAME(combo box) ,YEAR PUBLISHED(t.box) and SAVE button .the CATEGORY combo pickup data from CATEGORY TABLE on cat_name . on back end (SQL SERVER) there are two tbl i.e CATEGORY(cat_id,cat_name) and BOOKRECORD(book_name,cat_id,pub_year) Now when i press the SAVE button the record save in BOOKRECORD table with cat_id instead of selected category NAME. HOW can i accomplish this task plz HELP ME. THANK TO YOU ALL .

Member Avatar
Member Avatar
+0 forum 1

Hi, I had an assignment where we had to fox a program and get it working correctly. I fixed 99% of the problems and the program runs correct. The only thing that is wrong is my insertionSort() method. It runs correctly, but in the wrong order. Currently its listing names in descending order, comparing the last names (if last names are equal then compares first names). I have been messing around with my loops for a while now and cant figure out how to switch the order which it runs the sort. The fix is probably something really small and …

Member Avatar
Member Avatar
+0 forum 2

so i was trying to make a binary tree program. just inserting and deleteing. some how i messed up. i think insertion is ok till level 1 after that stays on level 1 only. also the function to delete the whole tree is not working. it only deletes node at level 0 only. first i thought that assigning the value to pointer as 0 will make it null pointer but i was not able to comapre it with 0. it was having some other value that compiler assigned it. i have tried to test it a lot of times but …

Member Avatar
Member Avatar
+0 forum 2

Hello there, I am new to C++. I have a task from school to make simple sorting program using Insertion Sort with a linked list. I would like to have comment, feedback, and correction of my code below. #include <iostream> using namespace std; struct Sort{ int value; Sort *next; // Point to the next address on this element }; int main(){ Sort *anchor; // A "bookmark" for pointer // Put into anchor everytime we make // another Sort // Use this pointer to arrange data Sort *anchor_temp; // To display the data after arrangement Sort *actual; // Actual position int …

Member Avatar
Member Avatar
+0 forum 2

Does dynamic keyword insertion on ads and landing pages guarantees high quality score? How can it generate positive effects on conversions?

Member Avatar
Member Avatar
+0 forum 8

void insertion_sort(int *a, int n) { int for(i=1;i<n;i++) { value=a[i]; for(j=i;j>0 && value<a[j-1];j--) a[j]=a[j-1]; a[j]=value; } } can anyone pls explain me the flow of this code... i am having confusion in understanding the flow..

Member Avatar
Member Avatar
+0 forum 4

I have a bit a of a problem because I have no idea what's going on in my code. Could someone guide me with this? **It has to swap the nodes themselves. ** #include <iostream> #include <iomanip> using namespace std; typedef struct node { int DATA; node *NEXT; }; node *HEAD = NULL; void Create(int data); void Display(); void Sort(); int main() { int num, numOfEl; cout << "Enter number of elements: "; cin >> numOfEl; cout << "Enter " << numOfEl << " numbers: "; for(int i = 0 ; i < numOfEl ; i++) { cin >> num; …

Member Avatar
Member Avatar
+0 forum 2

Hi, I'm learning C/C++ at school and we have our vacations; thought I might do some programs on my own. I've landed at trouble with sorting numbers in a 1-D array at the time of insertion. It seems to work up to a certain point, after which it fails to work logically. I have tried to find my problem, but I couldn't and would appreciate some help here. I have attached both the code and a sample output explaining my problem. [CODE]#include <iostream.h> #define size 20 void main () { int arr[size], count=-1, j, temp, flag, pos; char ch; do …

Member Avatar
Member Avatar
+0 forum 2

I wrote this program for insertion sort.I compiled the source file in xubuntu terminal.But when I try to run a.out executable....terminal showing "segmentation fault".I don't about segmentation fault..when segmentation fault occurs...what is the thing in the source file that is causing segmentation fault... Thanks in advance...... [CODE]#include<stdio.h> main(){ int i,j,n,key; int a[n]; printf("Enter size:"); scanf("%d",&n); printf("\n Enter elements:separate each by space\n"); for(j=0;j<n;j++) scanf("%d ",&a[j]); for(j=1;j<=n-1;j++){ key=a[j]; i=j-1; while((i>=0)&&(a[i]>key)){ a[i+1]=a[i]; i=i-1; } a[i+1]=key; } for(j=0;j<=n-1;j++) printf("%d ",a[j]); }[/CODE]

Member Avatar
Member Avatar
+0 forum 5

Hello, everyone!:) I am new to Daniweb and I would like a little help in implementing Binomial Heap subroutines in C, especially insertion in Heap. For my application it is necessary to implement max-heaps(i.e., roots storing the maximum value) in stead of min-heaps(i.e., root storing minimum value) in ANSI C. I have written a few subroutines - but things are not working properly. I will explain my codes here with code snippet and my problem. [CODE=C] struct _tnode{ int key;//a fi eld key for its key int degree;/*a fi eld degree for the number of children -or for the degree …

Member Avatar
+0 forum 0

Hi, I wrote a program that inserts nodes into a linked list in descending order.But whenever I test my code with numbers 12,14,13,19,7 in this order.Whenever I entered 7 I took 7 is already in the list.But as easily seen 7 is not in the list before I inserted.After give this error,if I choose print option by typing 2 my program entered in an infinite loop.I can not see my mistake and I am very confused.Please help me.My code is in the following: [CODE]include include struct node { int content; struct node *nextLink; }; typedef struct node NODE; void print(NODE …

Member Avatar
Member Avatar
+0 forum 3

The past few days I've posted some sorting routines customized to the tasks they were required to solve. Since I have a bunch of different sorting routines already coded, I decided to post them. Please note that some of these sorts are fast, but not a single one is as fast as the built in Array.Sort due to these using managed code while I suspect Array.Sort uses pointers and unmanaged code. The snippet above is the base class I use for all the sort routines to ensure that they are interchangeable. All the routines are generic sorts and will sort …

Member Avatar
Member Avatar
+3 forum 12

I'm using Datagridview in Windows application in C#. I've bound the datagridview with a table.Now on run time when the user inserts new row and presses up or down button,that row should automatically be inserted into database and if its not a new row then nothing should happen. Some1 suggested me RowValidating event but it dint help. Plzz can any1 help me how to implement this thing. -Thnx

Member Avatar
+0 forum 0

Is there a way to programmatically set the insert point in a multiline richTextbox? Example: I have a (almost) free format message where the reciever is expected to enter his own information at certain points (marked with a special char-sequence), before the message is processed further. A richTextbox.Find will find the spot in the message where the text is supposed to be entered, but I have not yet found a way to move the cursor to that particular position.

Member Avatar
Member Avatar
+0 forum 3

Can anyone help me fix this problem I have with my code. I am supposed to be writing a Binary Insertion Sort method that calls a binary search method. I have code that is working for in order integers, when tested on reverse order integers it works except it places the middle term + 1 at the end (If size was 1000, 501 would be at the end), and it does not work at all for random order. Here is the code: public static void sortBinary(Comparable [] x) { int insert,i,j; Comparable temp; for(i=1;i<x.length;i++) { insert = binarySearch(x,x[i]); if(insert < …

Member Avatar
+0 forum 0

the inputs are these computer 21 sabanci 4 sabanci 6 university 6 university 12 computer 17 university 3 computer 9 computer 6 sabanci 2 university 9 problem occurs at third line when the input name1 is equal with the previous input name1. Really thx for helping. Sorry for mess. #include <fstream> #include <string> #include <iostream> using namespace std; class linklist { public: linklist(); void InsertOrdered(string name, string key); // void PrintList(); ~linklist(); private: struct sublink { string key; sublink *next; sublink() { next = NULL; key = ""; } }; struct link { string name; sublink *subnext; link *mainnext; link() …

Member Avatar
Member Avatar
+0 forum 1

I was trying to Compare the 5 Sorting Algorithms using 4 test cases. [U]Sorting Algorithms[/U] [LIST=1] [*]Selection Sort [*]Insertion Sort [*]Bubble Sort [*]Quick Sort [*]Merge Sort [/LIST] [U]Test cases[/U] [LIST=1] [*][URL="http://the.nube.googlepages.com/Sort.Analysisplot1.svg"]Random Data [/URL](completed) [*]Reverse Sorted Data (completed) [*]Almost Sorted Data (can't generate Data) [*]Highly Repetitive Data (can't generate Data) [/LIST] At first I chose C++ which went to disaster and i finally end up using Octave. I successfully tested for [B][U]Random[/U] and [U]Reverse Sorted Data[/U] [/B]. But in other 2 Couldn't even generate data..... the what i have done so far have been attached with the post.... i have rename …

Member Avatar
Member Avatar
+1 forum 1

The End.