Difference between revisions of "Shell Sort"

From CodeCodex

m (New page: How to shell sort using C Language <highlightsyntax = Language = " C "> #include <string.h> #include <stdio.h> #include <stdlib.h> void shell(char *items, int count) { regis...)
 
 
Line 1: Line 1:
 +
===C===
 
How to shell sort using C Language
 
How to shell sort using C Language
  
<highlightsyntax = Language = " C ">
+
<pre class="C">
 
#include <string.h>
 
#include <string.h>
 
   #include <stdio.h>
 
   #include <stdio.h>
Line 38: Line 39:
 
   }
 
   }
  
</highlightsyntax>
+
</pre>
 +
 
 +
=== Tcl ===
 +
<pre class="tcl">
 +
proc shellsort args {
 +
set key 0
 +
set increment 3
 +
foreach arg $args {
 +
        set numbers($key) $arg
 +
        incr key
 +
}
 +
set number [array size numbers]
 +
 
 +
while {$increment > 0} {
 +
      for {set i 0} {$i < $number} {incr i} {
 +
        set j $i
 +
        set temp $numbers($i)
 +
        while {($j >=$increment) && ($numbers([expr $j-$increment]) > $temp)} {
 +
              set numbers($j) $numbers([expr $j - $increment])
 +
              set j [expr $j - $increment]
 +
        }
 +
        set numbers($j) $temp
 +
      }
 +
 
 +
      if {[expr $increment/2] != 0} {
 +
        set increment [expr $increment/2]
 +
      } elseif {$increment == 1} {
 +
        set increment 0
 +
      } else {
 +
        set increment 1
 +
      }
 +
 
 +
}
 +
for {set i 0} {$i < $number} {incr i} {lappend sort $numbers($i)}
 +
puts $sort
 +
}
 +
</pre>
 +
 
 +
[[Category:C]]
 +
[[Category:Tcl]]

Latest revision as of 18:47, 17 February 2011

C[edit]

How to shell sort using C Language

#include <string.h>
  #include <stdio.h>
  #include <stdlib.h>

  void shell(char *items, int count)
  {

    register int i, j, gap, k;
    char x, a[5];

    a[0]=9; a[1]=5; a[2]=3; a[3]=2; a[4]=1;

    for(k=0; k < 5; k++) {
      gap = a[k];
      for(i=gap; i < count; ++i) {
        x = items[i];
        for(j=i-gap; (x < items[j]) && (j >= 0); j=j-gap)
          items[j+gap] = items[j];
        items[j+gap] = x;
      }
    }
  }

  int main(void)
  {

    char s[255];

    printf("Enter a string:");
    gets(s);
    shell(s, strlen(s));
    printf("The sorted string is: %s.\n", s);

    return 0;
  }

Tcl[edit]

proc shellsort args {
 set key 0
 set increment 3
 foreach arg $args {
        set numbers($key) $arg
        incr key
 }
 set number [array size numbers]

 while {$increment > 0} {
      for {set i 0} {$i < $number} {incr i} {
         set j $i
         set temp $numbers($i)
         while {($j >=$increment) && ($numbers([expr $j-$increment]) > $temp)} {
               set numbers($j) $numbers([expr $j - $increment])
               set j [expr $j - $increment]
         }
         set numbers($j) $temp
      }

      if {[expr $increment/2] != 0} {
        set increment [expr $increment/2]
      } elseif {$increment == 1} {
        set increment 0
      } else {
        set increment 1
      }

 }
 for {set i 0} {$i < $number} {incr i} {lappend sort $numbers($i)}
 puts $sort 
}