如何优化单个链表上的插入排序程序? [英] How can this program of insertion sort on single linked list be optimized ?

查看:90
本文介绍了如何优化单个链表上的插入排序程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

{

Node i , j ;
Node key = start ;  

int k = size ;

for ( int p=1 ; p<=size ; p++ )
{
Node start1 = start ;
  
    if( p!=1 ) 
    {
        key = key.getNextNode() ; 
    }

int kk = 0;

j = key ;  
       
i = new Node ( key.data ) ;
for ( int c = p-1 ; c>=1  ; c--   )
{
start1 = start ; 
    
while ( start1.getNextNode() != j )    
{
    start1 = start1.getNextNode() ;
}
 
if ( start1.getdata() > i.getdata() ) 
{
start1.getNextNode().data = start1.data ;    

j=start1 ;
kk++ ;
}

else 
{
break;
}

}
  
if (kk!=0)
j.data = i.data ;





}



我的尝试:



我在单个链表上实现了Insertion Sort。如何在空间和代码行方面进一步改进这个程序?



}

What I have tried:

I have implemented Insertion Sort on single linked list . How this program can be improved further in terms of space and lines of code ?

推荐答案

学会正确缩进你的代码,它显示它的结构,它有助于阅读和理解。它还有助于发现结构错误。

Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
{

  Node i , j ;
  Node key = start ;

  int k = size ;

  for ( int p=1 ; p<=size ; p++ )
  {
    Node start1 = start ;

    if( p!=1 )
    {
      key = key.getNextNode() ;
    }

    int kk = 0;

    j = key ;

    i = new Node ( key.data ) ;
    for ( int c = p-1 ; c>=1  ; c--   )
    {
      start1 = start ;

      while ( start1.getNextNode() != j )
      {
        start1 = start1.getNextNode() ;
      }

      if ( start1.getdata() > i.getdata() )
      {
        start1.getNextNode().data = start1.data ;

        j=start1 ;
        kk++ ;
      }

      else
      {
        break;
      }

    }

    if (kk!=0)
      j.data = i.data ;



专业程序员的编辑器具有此功能,其他功能包括括号匹配和语法高亮。

Notepad++主页 [ ^ ]

ultraedit [ ^ ]


这篇关于如何优化单个链表上的插入排序程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆