KTUG마당은 KTUG를 방문하는 모든 이용자가 대화를 나누고 소식을 전하는 곳입니다.

  • 로그인 없이 자유롭게 글을 읽고 쓸 수 있는 철학은 처음과 같이 계속됩니다.
  • Team Blog의 글을 이곳 게시판의 "정보글"로 모았습니다. Team blog는 기고자가 올린 글에 질문을 받는 부담을 줄이기 위하여 댓글을 허용하지 않았습니다. 그러나 이곳 게시판으로 모으면서 댓글을 달 수 있습니다. 게시물을 작성하실 때 댓글을 원하지 않으시면 댓글을 허용하시지 않으시기를 바랍니다. 또한 불필요한 소모성 댓글을 달지 않도록 주의하여 주시기를 바랍니다.
  • TeX과 관련된 질문이나 답변은 QnA 마당을 이용하십시오. TeX과 관련된 질문은 지웁니다
  • MathJax를 이용한 수식조판을 사용하실 수 있습니다. 여기를 참조하세요.
  • 스팸 글을 막기 위하여 짧은 시간 내에 다시 글이 등록되는 IP를 막거나, 광고 글을 막기 위하여 금지어로 .com, .net 등을 설정하고 있습니다. 다소간의 불편함이 있으시더라도 양해 바랍니다.
    • 금지어에서 stackexchange, stackoverflow, ctan, overleaf, , github, google.com, gmail.com, .org, .io, sil.org, wiki.com, tistory.com등은 해제하였습니다.
  • 사용하는 편집기는 CKeditor입니다. 편집기에서 [enter]를 누르면 <p> 태그가 들어가고, 문단으로 생각하고 한줄을 비웁니다. 글줄만 바꾸려면 shift-enter 를 누르시면 <BR>가 들어가므로 용도에 맞게 나누어 쓸 수 있습니다.

정보글 문자열의 삽입정렬

2012.10.08 16:51

karnes 조회 수:5131 추천:1

문자열 정렬에 대해서는 DohyunKim 님의 http://www.ktug.or.kr/xe/index.php?document_srl=33728 이 있다.

다음 소스는 xfor 패키지에서 가져온 것으로 원래 숫자에 대해서만 삽입정렬하게 한 것인데 윗글의 \pdfstrcmp를 이용하여 문자열 정렬을 시도해보았다.

정렬 방식은 삽입정렬(insertion sort)


\documentclass{oblivoir}


\usepackage{xfor}


\makeatletter

\newcommand{\insertinto}[2]{%

\def\nlst{}% new list initially empty

\@for\n:=#2\do{%

% store new list in \toks@

\expandafter\toks@\expandafter{\nlst}%

% test current value against new value

%%%\ifnum\n>#1\relax %%%

\ifnum\pdfstrcmp{\n}{#1}=1\relax

% new value needs to be inserted before current value

\edef\newstuff{#1,\n}%

% end for loop at the end of this iteration

\@endfortrue

\else

\edef\newstuff{\n}%

\fi

% append new stuff to new list

\ifx\nlst\@empty

\edef\nlst{\newstuff}%

\else

\edef\nlst{\the\toks@,\newstuff}%

\fi

}%

% check to see if for loop was prematurely terminated

\if@endfor

% loop may have been terminated during final iteration, in

% which case \@forremainder is empty.

\ifx\@forremainder\@empty

% do nothing

\else

% loop prematurely ended, append remainder of original list

% to new list

\expandafter\toks@\expandafter{\nlst}%

\edef\nlst{\the\toks@,\@forremainder}%

\fi

\else

% wasn't prematurely terminated, so new value hasn't been added

% so add now.

\expandafter\toks@\expandafter{\nlst}%

\ifx\nlst\@empty

\edef\nlst{#1}%

\else

\edef\nlst{\the\toks@,#1}%

\fi

\fi

\global\let#2=\nlst

}


\newcommand*{\insertionsort}[1]{%

\def\sortedlist{}%

\@for\val:=#1\do{{\insertinto{\val}{\sortedlist}}}%

\let#1=\sortedlist

}


\makeatother


\begin{document}


\def\chlist{받,은,편,지,함,의,메,일,을,모,두,읽,으,셨,습,니,다}

\insertionsort{\chlist}

\chlist


\def\chlist{{독도는},{우리},{땅이다},{민족},{국가},{자유}}

\insertionsort{\chlist}

\chlist


\end{document}


결과:

outtag.PNG





XE Login