D1165: Inserting sort

Source: Internet
Author: User

Describe
Enter an integer of 10 0~100 into the array data, and use the insertion sort method to sort the input in ascending order and output

Input
10 Integers of 0~100
Output
10 number of inputs from small to large output
Sample input

7
3
6
12
2
98
15
60
20
16

Sample output

2 3 6 7 12 15 16 20 60 98

Idea: Use the chain list, edge comparison side insertion, the final output.

Code:

#include
using namespace Std;
struct node
{
int data;
Node *next;
};
Class Linklist
{
Private
Node *first;
Public
Linklist ();
~linklist ();
void Paixu (int x);
void Printlist ();
};
Linklist::linklist ()
{
First = new node;
First->next = NULL;
}

Linklist::~linklist ()
{
Node *q=null;
while (First!=null)
{
Q = First;
First = first->next;
Delete q;
}
}
void linklist::P aixu (int x)
{
Node *p,*q,*r;
R=new node;
R->data= x;
p = First;
Q = first->next;
if (q!=null)
{
while (Q!=null&&q->data<</span>r->data)
{
q=q->next;
p=p->next;
}//Edge Traverse Edge to compare new data
if (q!=null)
{
r->next=q;
p->next=r;
}//finding new data locations and not inserting new data at the end of the list
else if (q==null)
{
p->next=r;
r->next=null;
}//find new data location insert new data at the end of the list

}
else if (q==null)
{
p->next=r;
r->next=null;
}
}
void linklist::P rintlist ()
{
Node *p;
p=first->next;
while (P!=null)
{
cout<<p->data<<endl;
p=p->next;
}//output all sorted data
}
int main ()
{
Linklist l;
int i,x;
for (i=0;i<</span>10;i++)
{
cin>>x;
L.paixu (x);
}
L.printlist ();
return 0;
}

D1165: Inserting sort

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.