hi i need help!!
i need a program where you enter a string and it has to sort the string using insertion sort showing the words in descendent order

Recommended Answers

All 7 Replies


Posting requests for help must be well thought out if you want help quickly and correctly.  Your post did not meet the criteria for quality help. You may get some posts, but are they going to be useful?  Check your post with these checkpoints - what is it [i]you[/i] missed:
[*]Ask a question that can be answered. Do not ask
- What's wrong with my code?
- Why doesn't this work?
- Anything else that does not give us useful information.
[*]Post your code.  If we don't know what you did, how can we possibly help?
- Use [b]PROPER FORMATTING[/b] -- see this
- Use CODE Tags so your formatting is preserved.
If we can't follow your code, it's difficult to help. We don't care that you're still working on it. If you want us to read it, it must be readable
[*]Explain what the code is supposed to do.  If we don't know where the target is, how can we help you hit it?
[*]Explain what actually happened! If we don't know where the arrow went when you shot it, how can we tell what went wrong and how far from the target you are?
[*]If you have errors, post them! We can't see your screen.  We can't read your mind. You need to tell us what happened.
[*]Do [b]not[/b] ask for code. We are not a coding service. We will help you fix your code. 
    If anyone posts working code for you, they are a cheater. 
    If you use that code [i]you[/i] are a cheater.
[*]Do [b]not[/b] bore us with how new you are. We can tell by your code.
- Do not apologize. We were all new, and unless you are completely 
  brain dead you will get better.
- Do not ask us to "take it easy on you."
- Do not say "I don't know what's going on." That's obvious since
  you posted for help. Use that time wisely by [b]explaining[/b] as best 
  you can so we can help.
[*]Do not apologize for posting 'late'. We don't have any expectations on when you should be posting - 10 minutes or 10 days. We aren't timing your responses.
[*][b]Do not post your requirements and nothing else. [/b]We view that as a lazy do-nothing student that wants us to do their work for them. That's cheating and we [i]will[/i] be hard on you.
[*]Do not attach files except when absolutely necessary. Most of us are not going to download files.  Add the information to your post.
[*][b]Do not tell us how urgent it is.[/b] Seriously, for us there is no urgency at all. Many that can help will ignore any URGENT or ASAP requests.
[*]Create a [b][i]good[/i][/b] title for your post. The title [b]C++[/b] in the C++ forum is bloody redundant and worthless!  [b]What's wrong?[/b] equally so. What are you having trouble with? [i]There[/i] is your title. [i](note: [b]my program[/b] is not the answer.)[/i]
Think more about your next post so we don't have to play 20 questions to get the info we need to help you.


#include <iostream>
#include <conio.h>
#include <string.h>
using namespace std ;
char str1[20],str2[20],str3[20],str4[20],str5[20],comodin[20];
int main()
    gets (str1);
    gets (str2);
    gets (str3);
    gets (str4);
    gets (str5);
    int d = strcmp(str5,str4);
    if (d==-1){
        strcpy(str4,str5) ;
    int f = strcmp(str5,str3);
    if (f==-1){
        strcpy(str3,str5) ;
    int g = strcmp(str5,str2);
    if (g==-1){
        strcpy(str2,str5) ;
    int h = strcmp(str5,str1);
    if (h==-1){
        strcpy(str1,str5) ;
        int c = strcmp(str4,str3);
    if (c==-1){
        strcpy(str3,str4) ;
    int v = strcmp(str4,str2);
    if (v==-1){
        strcpy(str2,str4) ;
         int i = strcmp(str4,str1);
    int i = strcmp(str4,str1);    
    if (i==-1){
        strcpy(str1,str4) ;
    int b = strcmp(str3,str2);
    if (b==-1){
        strcpy(str2,str3) ;
    int z = strcmp(str3,str1);
    if (z==-1){
        strcpy(str1,str3) ;
    int a = strcmp(str2,str1);
    if (a==-1){
        strcpy(str1,str2) ;
    return 0;

this actually works , but it is not insertion sort , and i dont know how to do that

//Coded by: Lester Lucky M. Alvaran

#include <iostream>

using namespace std;

void main()
     char str[10], temp;
     cout<<"Enter string: ";
     for(int i=0; i<=10; i++)
         for(int j=i+1; j<=10; j++)
         cout<<i+1<<": "<<str<<endl;

test this

Unfortunately the code posted by jman2011 has multiple errors.

Assuming insertion sort means sorting occurs with insertion so the new entry is put in the correct place from the get go, then you could use either an array or a list or a vector or a tree. Given the hassle of shifting in an array on a frequent basis that would mean using an array would be a hassle to do this. If you can use vectors you might want to look into that. Using a tree to do this seems beyond your level of experience at this time. So I'd recommend using a list. You could use an STL list or write your own.

You start with an empty list. The first entry is straight forward. The second entry goes before the first if strcmp is less than one and after if greater than the first (you can decide what you want to do if youthe string is have duplicate entries). Then you could traverse the list until the string to insert is bigger than the string in the current node and less than the next node. Then insert the string to the right of the current node.

My apologies to jman2011. The code posted by jman2011 does come close to implementing a bubble sort using a string as a char array; but,
1) main() should have return type int not void and
2) it would be safer to use >> or getline() to get the input string rather than gets() and
3) the last valid index for an array of 10 items is nine so i should be < 10, not <= 10 and
4) you don't want to sort the newline char at the end of the string, but even if he had used an array of int rather than an array of char the loop using i shouldn't to the end of the array, but only to one less than the last item in the array to be sorted.

what your friend did was a bubble sort, here's d code you want-

char a[10], t;
int j=0;
cout<<"Enter string: ";
for(int i=1; i<10; i++)

My first post was correct in that insertion sort takes a given value and inserts it into an already sorted collection. It was wrong in that insertion sorts are routinely done on unsorted arrays. It was also wrong in that my description would create a sorted collection in ascending rather than descending order. Sorry. I seem to be apologizing a lot in this thread. Hopefuly, that means I'm learning new things, too.

Bubble sort: find the largest (or smallest) item in the collection an put it in the right place, repeat for the next largest (smallest) etc

Insertion sort: Take a given item and put it in the right place in the previously ordered collection. For practical purposes, sort the first 2 items in the array as desired (smallest first if sorting in ascending order and largest first if in descending order). Insert the next element in the the array into previously ordered section of the array in the proper positioj, and repeat until all sorted.

//insertion sort using descending order
for(i = 1; i < size of array; ++i)//controls which element to insert
  j = i
  //inner loop controls where to insert current element
  while j > 0 and element j is greater than the element immediately to it's left
    swap these two elements
    decrease j by one
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.